locked
Tuple Class RRS feed

  • Question

  • While browsing posts in this forum, I come to know about some Tuple Classes. Can anyone explain what these classes are can I create array of these classes. Please don't post links to MSDN library, just give me some information about them. Thanks.
    Tuesday, March 8, 2011 11:30 PM

Answers

  • Hello,

    Tuples are a quick way to define some kind of virtual table to quickly store some data (rows). Here are some Tuple examples: http://www.abhisheksur.com/2010/11/working-with-tuple-in-c-40.html.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    • Marked as answer by Mio_Miao Monday, March 21, 2011 5:29 AM
    Wednesday, March 9, 2011 8:36 AM
  • Hello DCEcoder,

    Tuples provide a convenient way in which to link several values or objects, without first creating a class or structure to hold them. There are two articles describe the Tuple Class in detail: http://www.blackwasp.co.uk/Tuples.aspx and http://www.davidarno.org/2010/04/28/a-guide-to-using-the-immutable-classes-tuple/. In addition, this one is about some of the benefits of using Tuple, you can find some examples in it: http://www.codeproject.com/KB/cs/TuplesInCSharp.aspx.

    The Tuple can have more complex items inside it, such as array. For example, create a four item Tuple with two arrays, and initialize those arrays inside the constructor invocation. Then pass the Tuple variable to another method:

        class Program
        {
            static void Main(string[] args)
            {
                // Create four item tuple; use var implicit type.
                var tuple = new Tuple<string, string[], int, int[]>("aa",new string[] { "bb", "cc" },2,new int[] { 4, 6 });
                // Pass tuple as argument.
                Tup(tuple);
            }
             static void Tup(Tuple<string, string[], int, int[]> tuple)
            {
                // Evaluate the tuple's items.
                Console.WriteLine(tuple.Item1);
                foreach (string value in tuple.Item2)
                {
                    Console.WriteLine(value);
                }
                Console.WriteLine(tuple.Item3);
                foreach (int value in tuple.Item4)
                {
                    Console.WriteLine(value);
                }
            }
        }

    output:

    aa
    bb
    cc
    2
    4
    6

    You could also refer to this: http://www.dotnetperls.com/tuple.

    Hope it will help you.

    Best Regards,

    Mio


    Mio Miao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mio_Miao Monday, March 21, 2011 5:29 AM
    Thursday, March 10, 2011 5:56 AM

All replies

  • Hello,

    Tuples are a quick way to define some kind of virtual table to quickly store some data (rows). Here are some Tuple examples: http://www.abhisheksur.com/2010/11/working-with-tuple-in-c-40.html.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    • Marked as answer by Mio_Miao Monday, March 21, 2011 5:29 AM
    Wednesday, March 9, 2011 8:36 AM
  • Hello DCEcoder,

    Tuples provide a convenient way in which to link several values or objects, without first creating a class or structure to hold them. There are two articles describe the Tuple Class in detail: http://www.blackwasp.co.uk/Tuples.aspx and http://www.davidarno.org/2010/04/28/a-guide-to-using-the-immutable-classes-tuple/. In addition, this one is about some of the benefits of using Tuple, you can find some examples in it: http://www.codeproject.com/KB/cs/TuplesInCSharp.aspx.

    The Tuple can have more complex items inside it, such as array. For example, create a four item Tuple with two arrays, and initialize those arrays inside the constructor invocation. Then pass the Tuple variable to another method:

        class Program
        {
            static void Main(string[] args)
            {
                // Create four item tuple; use var implicit type.
                var tuple = new Tuple<string, string[], int, int[]>("aa",new string[] { "bb", "cc" },2,new int[] { 4, 6 });
                // Pass tuple as argument.
                Tup(tuple);
            }
             static void Tup(Tuple<string, string[], int, int[]> tuple)
            {
                // Evaluate the tuple's items.
                Console.WriteLine(tuple.Item1);
                foreach (string value in tuple.Item2)
                {
                    Console.WriteLine(value);
                }
                Console.WriteLine(tuple.Item3);
                foreach (int value in tuple.Item4)
                {
                    Console.WriteLine(value);
                }
            }
        }

    output:

    aa
    bb
    cc
    2
    4
    6

    You could also refer to this: http://www.dotnetperls.com/tuple.

    Hope it will help you.

    Best Regards,

    Mio


    Mio Miao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mio_Miao Monday, March 21, 2011 5:29 AM
    Thursday, March 10, 2011 5:56 AM
  • Hi DCEcoder,

    Have you got any helpful suggestions to this problem? I would like to close this post becasue it is long here. If you have got some good solutions to this problem, please mark it as answer. If you need further assistance, please feel free to let us know or open a new thread to ask. Thanks.

    Best Regards,
    Mio


    Mio Miao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 16, 2011 11:23 AM