Equivalent of Tuple (.NET 4) for .NET Framework 3.5

后端 未结 6 1869
我在风中等你
我在风中等你 2020-11-29 04:05

Is there a class existing in .NET Framework 3.5 that would be equivalent to the .NET 4 Tuple?

I would like to use it in order to return several values from a method,

6条回答
  •  遥遥无期
    2020-11-29 04:56

    No, not in .Net 3.5. But it shouldn't be that hard to create your own.

    public class Tuple
    {
        public T1 First { get; private set; }
        public T2 Second { get; private set; }
        internal Tuple(T1 first, T2 second)
        {
            First = first;
            Second = second;
        }
    }
    
    public static class Tuple
    {
        public static Tuple New(T1 first, T2 second)
        {
            var tuple = new Tuple(first, second);
            return tuple;
        }
    }
    

    UPDATE: Moved the static stuff to a static class to allow for type inference. With the update you can write stuff like var tuple = Tuple.New(5, "hello"); and it will fix the types for you implicitly.

提交回复
热议问题