9

I need some data type like List<int , int , int ,string , int >. Of course I can implement , but is there something built in .net 3.5.

Thanks .

Night Walker
  • 20,638
  • 52
  • 151
  • 228
  • 1
    Usually you would create a wrapper class for your and then have a List. If you need performance seeking on one of the fields, then Dictionary – StuartLC Sep 19 '11 at 10:49
  • Yes in the system lib: Tuple tuple; Will work. Just enter the values in the constructor. But you can also create your own class or struct just for holding data. -1 because you should have found this really fast by looking yourself. – MrFox Sep 19 '11 at 10:53
  • 3
    @MrFox where in the `System` lib 2.0/3.5 you can find `Tuple<>`? – Firo Sep 19 '11 at 12:23
  • 2
    +1-ing the question to make up for MrFox's -1 (which is incorrect, not to mention rude). System.Tuple only occurs in .NET 4.0 and later. – Mike Loux Mar 10 '13 at 13:55

8 Answers8

15

No, there isn't anything in .NET 3.5. But rather than Tuple, have you considered implementing your own simple type which encapsulates the members you need? Usually that ends up giving more readable code than Tuple anyway - especially when you've got quite a lot of members, most of which have the same types.

It's a lot easier to understand:

foo(sale.AdultTickets, sale.ChildTickets, ...);

than

foo(sale.Item1, sale.Item2, ...);

It's a little bit more work, but it needn't be much more.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

I've seen a simple Tuple implementation for C# in Real World Functional Programming from Jon Skeet and Tomas Petricek. Until v4 there was no implementation provided.

Code can be downloaded at http://archive.msdn.microsoft.com/realworldfp/Release/ProjectReleases.aspx?ReleaseId=3674

Sascha
  • 10,231
  • 4
  • 41
  • 65
3

I implemented my own:

public class Tuple<T1, T2, T3, T4, T5>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public T3 Item3 { get; set; }
    public T4 Item4 { get; set; }
    public T5 Item5 { get; set; }

    public Tuple(T1 Item1, T2 Item2, T3 Item3, T4 Item4, T5 Item5)
    {
        this.Item1 = Item1;
        this.Item2 = Item2;
        this.Item3 = Item3;
        this.Item4 = Item4;
        this.Item5 = Item5;
    }
}
unruledboy
  • 2,455
  • 2
  • 23
  • 30
1

You can drag the Tuple out of the F# distribution. Download the Redistributable package from http://msdn.microsoft.com/en-us/library/ee829875(VS.100).aspx; reference FSharp.Core in your C# projcet and then:

namespace FSharpTest
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Tuple<string> aTuple;
        }
    }
}
Tim Barrass
  • 4,813
  • 2
  • 29
  • 55
0

AFAIK 3.5 doesn't support Tuples.

RubbleFord
  • 7,456
  • 9
  • 50
  • 80
0

I am afraid there isn't. There's the KeyValuePair for tuples but no built in triples, quadruples etc.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
0

Not per se, but an implementation of the Tuple<> generic class(es) is not too hard, albeit cumbersome for all the "overloads".

There is a project on codeplex, but I don't know how mature it is.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
-2

You can use my implementation :)

public class Tuple
{
    public static Tuple<T1> Create<T1>(T1 t1)
    {
        return new Tuple<T1>(t1);
    }

    public static Tuple<T1, T2> Create<T1, T2>(T1 t1, T2 t2)
    {
        return new Tuple<T1, T2>(t1, t2);
    }

    public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 t1, T2 t2, T3 t3)
    {
        return new Tuple<T1, T2, T3>(t1, t2, t3);
    }

    static Tuple<T1, T2, T3, T4> Create<T1, T2, T3, T4>(T1 t1, T2 t2, T3 t3, T4 t4)
    {
        return new Tuple<T1, T2, T3, T4>(t1, t2, t3, t4);
    }

    static Tuple<T1, T2, T3, T4, T5> Create<T1, T2, T3, T4, T5>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
    {
        return new Tuple<T1, T2, T3, T4, T5>(t1, t2, t3, t4, t5);
    }

    static Tuple<T1, T2, T3, T4, T5, T6> Create<T1, T2, T3, T4, T5, T6>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6)
    {
        return new Tuple<T1, T2, T3, T4, T5, T6>(t1, t2, t3, t4, t5, t6);
    }

    static Tuple<T1, T2, T3, T4, T5, T6, T7> Create<T1, T2, T3, T4, T5, T6, T7>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7)
    {
        return new Tuple<T1, T2, T3, T4, T5, T6, T7>(t1, t2, t3, t4, t5, t6, t7);
    }

    static Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> Create<T1, T2, T3, T4, T5, T6, T7, TRest>(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, TRest Rest)
    {
        return new Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>(t1, t2, t3, t4, t5, t6, t7, Rest);
    }
}

public class Tuple<T1> : Tuple
{
    public T1 Item1 { get; set; }

    public Tuple(T1 Item1)
    {
        this.Item1 = Item1;
    }
}

public class Tuple<T1, T2>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }

    public Tuple(T1 Item1, T2 Item2)
    {
        this.Item1 = Item1;
        this.Item2 = Item2;
    }
}

public class Tuple<T1, T2, T3>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public T3 Item3 { get; set; }

    public Tuple(T1 Item1, T2 Item2, T3 Item3)
    {
        this.Item1 = Item1;
        this.Item2 = Item2;
        this.Item3 = Item3;
    }
}

public class Tuple<T1, T2, T3, T4>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public T3 Item3 { get; set; }
    public T4 Item4 { get; set; }

    public Tuple(T1 Item1, T2 Item2, T3 Item3, T4 Item4)
    {
        this.Item1 = Item1;
        this.Item2 = Item2;
        this.Item3 = Item3;
        this.Item4 = Item4;
    }
}

public class Tuple<T1, T2, T3, T4, T5>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public T3 Item3 { get; set; }
    public T4 Item4 { get; set; }
    public T5 Item5 { get; set; }

    public Tuple(T1 Item1, T2 Item2, T3 Item3, T4 Item4, T5 Item5)
    {
        this.Item1 = Item1;
        this.Item2 = Item2;
        this.Item3 = Item3;
        this.Item4 = Item4;
        this.Item5 = Item5;
    }
}

public class Tuple<T1, T2, T3, T4, T5, T6>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public T3 Item3 { get; set; }
    public T4 Item4 { get; set; }
    public T5 Item5 { get; set; }
    public T6 Item6 { get; set; }

    public Tuple(T1 Item1, T2 Item2, T3 Item3, T4 Item4, T5 Item5, T6 Item6)
    {
        this.Item1 = Item1;
        this.Item2 = Item2;
        this.Item3 = Item3;
        this.Item4 = Item4;
        this.Item5 = Item5;
        this.Item6 = Item6;
    }
}

public class Tuple<T1, T2, T3, T4, T5, T6, T7>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public T3 Item3 { get; set; }
    public T4 Item4 { get; set; }
    public T5 Item5 { get; set; }
    public T6 Item6 { get; set; }
    public T7 Item7 { get; set; }

    public Tuple(T1 Item1, T2 Item2, T3 Item3, T4 Item4, T5 Item5, T6 Item6, T7 Item7)
    {
        this.Item1 = Item1;
        this.Item2 = Item2;
        this.Item3 = Item3;
        this.Item4 = Item4;
        this.Item5 = Item5;
        this.Item6 = Item6;
        this.Item7 = Item7;
    }
}

public class Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>
{
    public T1 Item1 { get; set; }
    public T2 Item2 { get; set; }
    public T3 Item3 { get; set; }
    public T4 Item4 { get; set; }
    public T5 Item5 { get; set; }
    public T6 Item6 { get; set; }
    public T7 Item7 { get; set; }
    public TRest Rest { get; set; }

    public Tuple(T1 Item1, T2 Item2, T3 Item3, T4 Item4, T5 Item5, T6 Item6, T7 Item7, TRest Rest)
    {
        this.Item1 = Item1;
        this.Item2 = Item2;
        this.Item3 = Item3;
        this.Item4 = Item4;
        this.Item5 = Item5;
        this.Item6 = Item6;
        this.Item7 = Item7;
        this.Rest  = Rest;
    }
}
  • Check the answers bellow and see the quantity of code and the readability. – bitoiu Sep 19 '14 at 13:45
  • 1
    @bitoiu are you aware that this the same as the microsoft implementation? [MSDN](http://msdn.microsoft.com/en-us/library/system.tuple%28v=vs.110%29.aspx) – Jacinto Gomez Sep 19 '14 at 13:53
  • @bitoiu i have checked the answers below but non of them are compatible with the microsoft implementation, maybe this [MSDN](http://msdn.microsoft.com/en-us/library/system.tuple%28v=vs.110%29.aspx) link help you clarify what i wrote. – Jacinto Gomez Sep 19 '14 at 13:59