C# Or in string
hi
I am a complete programming noob. I have only recently started on c# and am terrible. I can write stuff and ask stuff and use strings to an extent.
I am looking for a way to write or in this sort of context
System.Console.Out.WriteLine("what is your name");
name = System.Console.Out.ReadLine();
if (name.Equals("Bob")
{
//do something here
}
//or
if (name.Equals("bob");
{
//do the same here
}
I want to merge the two together so i don't have to write the same thing out for each one.
I am a complete programming noob. I have only recently started on c# and am terrible. I can write stuff and ask stuff and use strings to an extent.
I am looking for a way to write or in this sort of context
System.Console.Out.WriteLine("what is your name");
name = System.Console.Out.ReadLine();
if (name.Equals("Bob")
{
//do something here
}
//or
if (name.Equals("bob");
{
//do the same here
}
I want to merge the two together so i don't have to write the same thing out for each one.
0
Comments
Your best bet is to use the 'or' comparison. With this, you can join the two statements. So, you should be able to put:
However, this only covers two cases. What if they put in bOb? Then this doesn't catch it. So, I would try this:
C# is an awesome language. Good luck!
System.Console.Out.WriteLine("what is your name");
name = System.Console.Out.ReadLine();
if (name.Equals("Bob")
{
//do something here
}
//or
else if (name.Equals("bob");
{
//do the same here
}
I'm not sure if the "or" operator is actually valid in C#, but I can say for certain that "||" is the common approach to making a conditional-OR statement.
Such as:
The advantage to the conditional-OR is that it short-circuits the statement, so if the left side is true, the right side is never evaluated. MSDN has some basic references for Operators, |, and ||.
Best of luck with your C# adventures!
but how about "or" for more than two different things which is not just about which case it is in?
:-)
for example
if (name.Equals("Bob") (//and) home.Equals("london"))
{
//do something here
}</pre>
;-)
StringComparison in this case is simply an enum that specifies the type of matching to do, either word or ordinal sorting and whether or not to take case into account.