Case sensitiveness (For Console Application)

My current code:

string math;
<insert stuff>
math = Console.ReadLine();
if (math == "add")
{
<Insert stuff>
}

My problem is that it only triggers if you type add, not when you type ADD or whatever, any idea how I would go about this? I know it has something to do with ToLower, but I don't know what to do with it. I tried to do math.ToLower(Console.Readline()); and vice versa which of course didn't work :(.

[462 byte] By [Blizzfury] at [2007-12-25]
# 1

ToLower does not modify the string argument but it returns a new string (and all other string functions behave like this, strings are imutable) so it should be:

math = Console.ReadLine();

math = math.ToLower();

if (math == "add") ...

MikeDanes at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

Try this:

math = Console.ReadLine();
if ((math.ToLower()) == "add")
{
<Insert stuff>
}

I hope it works ;-)

RizwanSharp at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 3
Ahaha that solved it, thanks guys and thanks for the info Mike :).
Blizzfury at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 4

You are Always Welcome ;-)

Cheers!!!

RizwanSharp at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 5
A more efficient way to do this is to use:

// Instead of this: if ((math.ToLower()) == "add")
// Use this:

if (string.Compare(math., "add", true) == 0)

MatthewWatson at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 6

>> A more efficient way to do this is to use:

More efficient if you're doing just the one comparison. However, there's a good chance he'll want to do several, in whcih case the best solution would be:

string math = Console.ReadLine();
math = math.ToLower();
if (math == "add")
///
else if (math == "sub")
///
else if (math == "times")

JamesCurran at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 7
Well if you're going to compare multiple values like that, an even clearer way would be to use a switch statement:


class="txt4">

string math = Console.ReadLine();
math = math.ToLower();

switch (math)
{
case "add":
...
break;

case "sub":
...
break;

case "times":
...
break;
}

MatthewWatson at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...