Thursday, April 1, 2010

.NET 4.0 : System.Tuple

So, let’s go back to our subject, .NET 4.0 new stuff & features.

One of the newest things that we will have in .NET 4.0 is a tuple, what’s right and it’s not a joke!

A tuple is data structure which can contain different types of data coupled. This is what makes it different from a List or other generic types.

Note: Do not confuse it with Microsoft.Dynamics.Framework.Reports.Tuple. It is another type.

So a tuple which will contain to 7 elements is created with a factory. System.Tuple.Create. A tuple with 8 elements will contain 7 elements and another tuple Rest.

Why would we need this  type? Well, this allows us to return two or more values of different types.

Here is the code which i made to play a little bit with this type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Intro.Tuple
{
    class Program
    {
        static void Main(string[] args)
        {
            var squaresList = System.Tuple.Create(1, 4, 9, 16, 25, 36, 49, 64);

            Console.WriteLine("1st item: {0}", squaresList.Item1);
            Console.WriteLine("4th item: {0}", squaresList.Item4);
            Console.WriteLine("8th item: {0}", squaresList.Rest);
            Console.ReadKey();

            Console.WriteLine(Environment.NewLine);
            
            var tupleWithMoreThan8Elements =
                System.Tuple.Create("is", 2.3, 4.0f,
                new List { 'e', 't', 'h' }, "udevi", new Stack(4), "best", squaresList);

            // we'll sort the list of chars in descending order
            tupleWithMoreThan8Elements.Item4.Sort();
            tupleWithMoreThan8Elements.Item4.Reverse();

            Console.WriteLine("{0} {1} {2} {3}", tupleWithMoreThan8Elements.Item5,
                tupleWithMoreThan8Elements.Item1,
                string.Concat(tupleWithMoreThan8Elements.Item4),
                tupleWithMoreThan8Elements.Item7);

            Console.WriteLine("Rest: {0}", tupleWithMoreThan8Elements.Rest);
            Console.WriteLine("Rest's 2nd element: {0}", tupleWithMoreThan8Elements.Rest.Item1.Item2);
            Console.WriteLine("Rest's 5th element: {0}", tupleWithMoreThan8Elements.Rest.Item1.Item5);
            Console.ReadKey();

            Console.WriteLine(Environment.NewLine);

            var tuple = GetNameAndAge();

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Name: {0}", tuple.Item1);
            Console.WriteLine("Age: {0}", tuple.Item2);
            Console.ReadKey();
        }

        private static Tuple GetNameAndAge()
        {
            var resultingTuple = System.Tuple.Create(string.Empty, 0);

            Console.WriteLine("Enter your name please: ");
            string name = Console.ReadLine();
            Console.WriteLine("Enter your age please: ");
            string ageValue = Console.ReadLine();

            int age = -1;

            if (int.TryParse(ageValue, out age))
            {
                resultingTuple = new Tuple(name, age);
            }

            return resultingTuple;
        }
    }
}

And here is what in our case the Rest is:

And the output:

Hope it helps you! See you around!

No comments:

Post a Comment