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

前端 未结 12 1729
别那么骄傲
别那么骄傲 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-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 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.

提交回复
热议问题