Will a future version of .NET support tuples in C#?

前端 未结 12 1673
别那么骄傲
别那么骄傲 2020-11-28 23:54

.Net 3.5 doesn\'t support tuples. Too bad, But not sure whether the future version of .net will support tuples or not?

相关标签:
12条回答
  • 2020-11-28 23:57

    I'd be surprised - C# is a strongly-typed language, whereas tuples are suited for more dynamically typed languages. C# has been drifting more dynamic as time goes on, but that's syntactic sugar, not a real shift in the underlying data types.

    If you want two values in one instance, a KeyValuePair<> is a decent substitute, albeit clumsy. You can also make a struct or a class that'll do the same thing, and is expandable.

    0 讨论(0)
  • 2020-11-29 00:04

    Implementing Tuple classes or reusing F# classes within C# is only half the story - these give you the ability to create tuples with relative ease, but not really the syntactic sugar which makes them so nice to use in languages like F#.

    For example in F# you can use pattern matching to extract both parts of a tuple within a let statment, eg

    let (a, b) = someTupleFunc
    

    Unfortunately to do the same using the F# classes from C# would be much less elegant:

    Tuple<int,int> x = someTupleFunc();
    int a = x.get_Item1();
    int b = x.get_Item2();
    

    Tuples represent a powerful method for returning multiple values from a function call without the need to litter your code with throwaway classes, or resorting to ugly ref or out parameters. However, in my opinion, without some syntactic sugar to make their creation and access more elegant, they are of limited use.

    0 讨论(0)
  • 2020-11-29 00:05

    There is a proper (not quick) C# Tuple implementation in Lokad Shared Libraries (Open-source, of course) that includes following required features:

    • 2-5 immutable tuple implementations
    • Proper DebuggerDisplayAttribute
    • Proper hashing and equality checks
    • Helpers for generating tuples from the provided parameters (generics are inferred by compiler) and extensions for collection-based operations.
    • production-tested.
    0 讨论(0)
  • 2020-11-29 00:05

    My open source .NET Sasa library has had tuples for years (along with plenty of other functionality, like full MIME parsing). I've been using it in production code for a good few years now.

    0 讨论(0)
  • 2020-11-29 00:05

    If I remember my Computer Science classes correctly tuples are just data.

    If you want grouped data - create classes that contain properties. If you need something like the KeyValuePair then there it is.

    0 讨论(0)
  • 2020-11-29 00:08

    To make these useful in a hashtable or dictionary, you will likely want to provide overloads for GetHashCode and Equals.

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