Is there a way in C# to use "var foo" but still having a named tuple?
(int first, int second) foo = "bar" switch
{
_ => (0, 0)
};
Maybe some place where the switch itself can define the return type?
CodePudding user response:
You can put a cast before the switch
:
var foo = ((int first, int second))("bar" switch
{
_ => (0, 0)
});
Or you can cast each arm of the switch (you'd have to do this for each arm):
var foo = "bar" switch
{
_ => ((int first, int second))(0, 0)
};
or specify the names of the tuple elements in each arm (again, you'd have to do this for each arm):
var foo = "bar" switch
{
_ => (first: 0, second: 0)
};
But either way, this is longer than using (int first, int second) foo
, so I don't recommend this.