Tuesday, March 9, 2010

.NET 4.0 Tuple<T>

In .NET 4.0 there is a new feature called  Tuple. First let us see the definition for Tuple .

What is Tuple?

In mathematics and computer science a tuple represents the notion of an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a positive integer. [Source Wikipedia]

Tuple is a factory class that provides static methods for creating instances of the tuple types supported by the .NET Framework. It provides helper methods that can be called to instantiate particular tuple objects without requiring that you explicitly specify the type of each tuple component.

Let’s take a look at the following  simple math example.

image

In the above  example i have created a method which returns a Tuple and  which has three Integer values. So using Tuple a method can returns multiple values  without using out parameters.

To access values in aTuple we use ItemN where N is the nth position in a Tuple. In the above example  I am  accessing the  Item1 value that returns me  the first value of num1+num2+num3 from Tuple. like that we can access Tuple values .

Tuple is not new concept the language like Python and Haskell has this feature as well.

Nandri(Thanks)

SreenivasaRagavan.

No comments: