Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C# 4.0

An introduction to Tuple

Rate me:
Please Sign up or sign in to vote.
2.93/5 (27 votes)
25 Oct 2010CPOL4 min read 49.8K   111   2  
This article will explain about some of the benefits of using Tuple in C#4.0
using System;
using System.Reflection;
using System.Linq;

namespace CSharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 5;

            #region Multiplication Table without REFLECTION 
            var tuple = MultiplicationTable(number);

            string format1 =
               "Multiplcation Table of {0} is" + Environment.NewLine +
               "------------------------------" + Environment.NewLine + Environment.NewLine +
               "{0} * 1     = {1} " + Environment.NewLine +
               "{0} * 2     = {2} " + Environment.NewLine +
               "{0} * 3     = {3} " + Environment.NewLine +
               "{0} * 4     = {4} " + Environment.NewLine +
               "{0} * 5     = {5} " + Environment.NewLine +
               "{0} * 6     = {6} " + Environment.NewLine +
               "{0} * 7     = {7} " + Environment.NewLine +
               "{0} * 8     = {8} " + Environment.NewLine +
               "{0} * 9     = {9} " + Environment.NewLine +
               "{0} * 10    = {10} " + Environment.NewLine;

            string result = string.Format(
                format1
                , number
                , tuple.Item1
                , tuple.Item2
                , tuple.Item3
                , tuple.Item4
                , tuple.Item5
                , tuple.Item6
                , tuple.Item7
                , tuple.Rest.Item1
                , tuple.Rest.Item2
                , tuple.Rest.Item3

                );
            Console.WriteLine(result);

            #endregion

            #region Multiplication table using REFLECTION

            string format2 =
               "Multiplcation Table of {0} is" + Environment.NewLine +
               "------------------------------" + Environment.NewLine ;
            Console.WriteLine(format2, number);
            string[] multipliers = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
            
            Type t = typeof(CSharpDemo.Program);
            
            t.InvokeMember(
            "MultiplicationTable"
            , BindingFlags.InvokeMethod
            , null
            , Activator.CreateInstance(t)
            , new object[] { number })
            .ToString()
            .Replace("(", string.Empty)
            .Replace(")", string.Empty)
            .Split(',')
            .Zip(multipliers,(first, second) => number.ToString() + "*" + second + "=" + first)
            .ToList().ForEach(i => Console.WriteLine(i));

            #endregion

            #region TupleExtension To get the Tuple length and Item Values
            Tuple<int,object> testTuple = new Tuple<int,object> (0,null);
            var resultantTuple = testTuple.GetLengthAndMemeberValues("MultiplicationTable", new object[] { number });
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("Number of TupleItems" + resultantTuple.Item1);
            Console.WriteLine("Items of Tuple" + resultantTuple.Item2);
            #endregion

            Console.ReadKey(true);
        }

        /// <summary>
        /// MultiplicationTable
        /// </summary>
        /// <returns></returns>
        public static Tuple<int,int,int,int,int,int,int,Tuple<int, int, int>> 
            MultiplicationTable(int number)
        {
           Tuple<int, int, int, int, int, int, int, Tuple<int, int,int>> multiplicationTable =
                new Tuple<int, int, int, int, int, int, int, Tuple<int, int,int>>
                (
                 number * 1
                , number * 2
                , number * 3
                , number * 4
                , number * 5
                , number * 6
                , number * 7
                , new Tuple<int, int, int>(number * 8, number * 9, number * 10));
           return multiplicationTable;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions