Can I have a switch statement like this:
...
switch (temp)
{
case "NW" or "New":
temp = "new stuff"
break;
}
...
Can I have a switch statement like this:
...
switch (temp)
{
case "NW" or "New":
temp = "new stuff"
break;
}
...
No, but you can use (at least in Java)
switch (temp) {
case "NW":
case "New":
temp="new stuff";
break;
}
Yes. This is how it is done.
switch (temp)
{
case "NW":
case "New":
temp = "new stuff"
break;
}
Actually, I answered this very same question before.
Assuming C#, you want:
switch(temp)
{
case "NW":
case "New":
temp = "new stuff";
break;
}
switch (temp) {
case "NW":
case "New":
temp = "new stuff"
break;
default:
Console.WriteLine("Hello, World!");
break;
}
I know you asked about C#, and have good answers there, but just for perspective (and for anyone else reading that might find it useful), here's the VB answer:
Select Case temp
Case "NW", "New"
temp = "new stuff"
Case Else
'something else...
End Select
Notice that there's no "break"--VB does not drop through cases. On the other hand, you can have multiple match conditions on a single case.
Be care you DON'T do this
...
Case "NW" Or "New"
...
What you have there is a single condition with a bitwise Or between the two terms....