With C# 7 being implemented, Tuples got a LOT easier to use. In fact, I have a case where I'll be using an identical Tuple signature for a few methods.
To save on typing, can I name or alias it? Or should I just go ahead and define a struct?
With C# 7 being implemented, Tuples got a LOT easier to use. In fact, I have a case where I'll be using an identical Tuple signature for a few methods.
To save on typing, can I name or alias it? Or should I just go ahead and define a struct?
You can use alias with value tuples like with any other type:
> using T = System.ValueTuple<int, string>;
> T t;
> t.Item1 = 1;
> t.Item2 = "one";
But that's probably not what you're looking for. And that's because the tuple value names are not in the type itself but in the variables and return values.
In the case of variables, the compiler keeps track of the value names. In the case of return values, the [System.Runtime.CompilerServices.TupleElementNamesAttribute] attribute is used to attach the names to the return value.
No you can not make a alias. If you want some kind of "short form" you will need to define an actual type and use that instead of the tuple.
Having read more of the existing duplicates, there's an important point which I think should be mentioned: Tuples in C# 7 are syntactic sugar, making Structs easier to use as one-offs.
That being the case, if a particular structure of tuple is going to be reused enough to need a name, then the full Struct syntax would be the appropriate solution.