What's the purpose of the Tuple(T1)/Singleton in .net?

后端 未结 4 1746
难免孤独
难免孤独 2020-12-03 13:06

One of the Tuple Types in .net 4 is a Single-Element Tuple. I just wonder what the purpose of this struct is?

The only use I saw is when using in the 8+ Tuple as it

相关标签:
4条回答
  • 2020-12-03 13:45

    All tuple types implement ITuple, so I guess that when you want to have a return type that is an ITuple, you need the option of having a single value, as you suggest.

    0 讨论(0)
  • 2020-12-03 13:47

    Tuples automatically implement IStructuralComparable and IStructuralEquatable, among other things. This allows tuples to be compared and sorted right out of the box. From Bill McCarthy's article in December 2009 edition of Visual Studio Magazine, "Types and Tuples in .NET 4":

    Although tuples may look simple and nondescript, they do provide strong typing and important comparison and equality functionality. Tuples are useful across method, class or even machine boundaries.

    By putting your data type into a tuple, even of only one element, you are guaranteed immutability, equatability, and comparability. For tuples consisting of only one element, the main benefit of using a tuple is going to be immutability: once the tuple is created, it's data can never change for the life of the tuple.

    0 讨论(0)
  • 2020-12-03 14:07

    Although this is an old thread, I was preparing an answer for Why use a 1-tuple, Tuple<T1> in C#? , which is now marked as a duplicate.

    One possible usage of a 1-Tuple, is to return a null instance if there is no value, but that null of the item value itself, does not mean there is no result.

    For example (not a very good real life example, but hopefully it conveys the point)

        static List<string> List = new List<string> {"a", "b", null, "c"};
    
        static Tuple<string> GetItemAtIndex(int index)
        {
            if (index < 0 || index >= List.Count)
                return null;
            return Tuple.Create(List[index]);
        }
    

    The calling code would know that when the tuple itself is null, it did not exist in the list, but if Item1 is null, there actually was a null item in the list. Of course in this example, plenty of better workarounds exist, but it could also apply to a result out of a database: no result found or a result that is null.

    Extending that same logic, a Tuple can be used to easily create generic methods that can return null for any type. For example

        static Tuple<T> GetSomething<T, RecType>(RecType rec, Func<RecType, T> fn)
        {
            if (rec == null) return null;
            //extra example, access would be checked by something like CheckCurrentUserAccess(rec)
            bool AccesDenied = true;
            if (AccesDenied) return null; //sometimes you don't want to throw an exception ;)
    
            return Tuple.Create(fn(rec));
        }
    

    You could return Nullable<T>, but that would only work for structs, where this works with any type, albeit int, string, int?, MyClass, etc

    0 讨论(0)
  • 2020-12-03 14:08

    You'd have to ask the BCL designers to be certain, but I suspect that since there is a 1-tuples in the real world, the .NET framework authors wanted to provide equivalent symmetry in their implementation.

    Tuples are the .NET implementation of what you'd consider the mathematical concept of a tuple.

    Now since you're asking for programming uses for Tuple<T>, I would also answer that there are .NET languages (like F#) that could use Tuple<> for representation of things like return values from functions. Since an F# function could certainly return a 1-tuple as a result - it adds symmetry and consistency to the behavior and feel of the language.

    Your example with 8+ tuples is also probably legitimate, in that the Rest property could be a 1-tuple to represent the "overflow".

    0 讨论(0)
提交回复
热议问题