Pages

Monday, March 9, 2009

Tuple in C# 4.0

C# 4.0 include a new feature called Tuple. In mathematics tuple is a ordered list of specific number of values called the components of the tuple. For example a 3-tuple name may be used as: (First-Name, Middle-Name, Last-Name).

Let's take a look in the following example:

        public Tuple<int, int> GetDivAndRemainder(int i, int j)

        {

            Tuple.Create(i/j, i%j);

        }

        public void CallMethod()

        {

            var tuple = GetDivAndRemainder(10,3);

            Console.WriteLine("{0} and {1}", tuple.item1, tuple.item2);

        }

 

In the above example the method can return a tuple which has two integer values. So using tuple we can return multiple values. So this will help lazy programmers to write less code but do more. One great use of tuple might be returning multiple values from a method.


To get more info on Tuple visit the following links:

http://en.wikipedia.org/wiki/Tuple

http://peisker.net/dotnet/tuples.htm

http://spellcoder.com/blogs/dodyg/archive/2008/10/30/16319.aspx

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.