'How To Read A Simple Switch In C# Short Hand
I am looking at some C# code that looks foreign to me
return Con.Value switch
{
CTypes.ISO => 776,
CTypes.HiCube => 888,
_ => 983,
};
Can this be translated to...?
int value = 0;
If(CTypes.ISO)
{
value = 776;
}
else if (CTypes.HiCube)
{
value = 888;
}
else()
{
value = 983;
}
Or is the code above something else?
Solution 1:[1]
Here you can see new syntax of switch.
For your question it shows exactly this
switch(Con.Value)
{
case CTypes.ISO:
return 776;
case CTypes.HiCube:
return 888;
default:
return 983;
}
And for your if else statement
int value = 0;
if(Con.Value == CTypes.ISO)
{
value = 776;
}
else if (Con.Value == CTypes.HiCube)
{
value = 888;
}
else
{
value = 983;
}
return value;
Solution 2:[2]
Yes, you're almost right.
Alternative is below:
int value = 0;
if(Con.Value == CTypes.ISO)
{
value = 776;
}
else if (Con.Value == CTypes.HiCube)
{
value = 888;
}
else
{
value = 983;
}
return value;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | juharr |
| Solution 2 | excommunicado |
