Is it possible in C# to define a variable like so?
switch(var num = getSomeNum()) {
case 1: //Do something with num
break;
default: break;
}
public static int GetSomeNum() => 3;
Is it possible in C# to define a variable like so?
switch(var num = getSomeNum()) {
case 1: //Do something with num
break;
default: break;
}
public static int GetSomeNum() => 3;
The documentation says that
In C# 6, the match expression must be an expression that returns a value of the following types:
a char.
a string.
a bool.
an integral value, such as an int or a long.
an enum value.
Starting with C# 7, the match expression can be any non-null expression.
To answer your question,
Yes you can switch on an int, e.g.
int myInt = 3;
switch(myInt)
Yes you can switch on the result of a method that returns an it, e.g.
int GetMyInt()
{
// Get my Int
}
switch(GetMyInt())
Yes you can switch on variable populated with a method result, e.g.
int GetMyInt()
{
// Get my Int
}
int myInt = GetMyInt();
switch(myInt)
No you can't do it like that.