Home > .NET 4.0, C# > .NET 4.0 – What’s new – Tuples #1

.NET 4.0 – What’s new – Tuples #1

October 10, 2009 Leave a comment Go to comments

A long time ago I’ve installed Visual Studio 2010 beta version. This was when I had Windows7 Beta 2 on my laptop. I was so excited but… unfortunately I was really disappointed, because every time I created a new class, VS hanged. This changed when I installed W7 RTM. From this time I am able to create some classes and applications (but this doesn’t means that VS is stable).

I found some new features in the .NET 4.0 – one of them is the Tuple class. It was for me very interesting – what is it (what does this mean in English). On Wiki I found an mathematical definition. But I couldn’t found for what purposes would be this class helpful in business. There is an example on the msdn pages, but still they don’t satisfy me. Searching a ton of pages and another examples (defining the tuple) I found that this class will be helpful in many situations – and one will be helpful in my job.

Short definition: Tuple is an ordered set of n-element objects. (Really short definiton).

In C# we can define a Tuple class with 1 to 7 elements. But we can extend it and have no limitations (more on msdn pages).

You can use it to retrieve data from database. Because the Tuple class is a generic class, a kind of collection class and also is an factory class. You are able to define many kinds of hardly defined types. For example You have a record in the database with 3 columns: first an integer, datetime and some text. You can define such Tuple

Tuple<int,DateTime,string> tuple;

Another example is to use this class as an key of an dictionary class. The list contains a set of Tuple<int,string> objecst like ID and NAME (described example below).

Dictionary<Tuple<int, string>, object> dict = new Dictionary<Tuple<int, string>, object>(); 

foreach (var key in list)
{
  if (dict.ContainsKey(key)) continue;
  dict.Add(key, null);
}

This example would be helpful in my job and in previous projects. Working in banks and some other business-jobs, you work with tons of client data like client name, id, account and so on. A tuple can identify a client by his Id and Name and by this a Tuple<ID,NAME> is an unique key of Your dictionary. Of course this is really a simple simple simple example. The big advantage of this is that You are disencumbered from writing some kinds of get hash methods, because the Tuple class do this for You.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: