-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Description
Dynamic languages such as python support multi-value assignments based on tuple, which is useful especially for multi return value functions.
In C#, there can be a similar implementation:
Tuple<TypeA, TypeB> F();
var a, b = F(); //a,b are defined by var
a,b = F(); //a, b are defined previously
var a, = F(); //only fetch the first part
var ,b = F(); //only fetch the second part
For readability, we can also specify the type explicitly,
var TypeA a, TypeB b = F();
or just var TypeA a,b = F(); // if typeA=typeB
The using of var is to let it easily distinguishable from the normal case: TypeA a, b=somevalue;
Of cause, the language details can still be discussed.
I know that C# support out keyword to return multi values, but in many cases, e.g. async/await we cannot use out keyword. Besides, this is a more elegant way to enable a function to return multi values.