开发者

Is there something like tuple in .net 3.5 [duplicate]

开发者 https://www.devze.com 2023-04-06 08:22 出处:网络
This question already has answers here: Equivalent of Tuple (.NET 4) for .NET 开发者_开发技巧Framework 3.5
This question already has answers here: Equivalent of Tuple (.NET 4) for .NET 开发者_开发技巧Framework 3.5 (6 answers) Closed 7 years ago.

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 .


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.


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


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;
    }
}


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;
        }
    }
}


AFAIK 3.5 doesn't support Tuples.


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


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.


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消