I'm working on my C# project,
and I need to do this, but without the If
, in a short way;
for (String = n) do that
for (String = s) do those
for (String = c) do this
I don't know how it's called, or how to do it without the long If
functions.
I'm working on my C# project,
and I need to do this, but without the If
, in a short way;
for (String = n) do that
for (String = s) do those
for (String = c) do this
I don't know how it's called, or how to do it without the long If
functions.
You can simply use a switch statement:
switch (myString) {
case "n":
//...
break;
case "s":
//...
break;
case "c":
//...
break;
}
But be aware that the used case
-strings are constants and case-sensitive!
Be also aware that using myString.ToLower()
is dangerous!
If case-sensivity is a problem, you have to use if
s:
if (string.Compare(myString, "abc", true)==0)) {
//...
}
else if (string.Compare(myString, "123", true)==0)) {
//...
}
You can use switch
, e.g. for a variable called myString
:
switch (myString) {
case "n":
doThat();
break;
case "s":
doThose();
break;
case "c":
doThis();
break;
default:
doNothing();
break;
}
The default
case is there in case your myString
is none of the values you have taken account of in your specific case
statements.