Use string.Equals
with an appropriate StringComparison
if (string.Equals(name, "ashley", StringComparison.CurrentCultureIgnoreCase))
{
...
}
If you know that the variable is not null you can also use
if (name.Equals("ashley", StringComparison.CurrentCultureIgnoreCase))
{
...
}
To answer your question in the comments, a do-while
loop can be used to loop until the question is answered correctly. The below will loop until the user enters something other than ashley
.
string name;
do
{
Console.WriteLine("Enter Name");
name = Console.ReadLine();
}
while (string.Equals(name, "ashley", StringComparison.CurrentCultureIgnoreCase));
You could combine this with a guard variable if you want different messaging:
string name;
bool nameIsCorrect = false;
do
{
Console.WriteLine("Enter Name");
name = Console.ReadLine();
nameIsAshley = string.Equals(name, "ashley", StringComparison.CurrentCultureIgnoreCase);
if (nameIsAshley)
{
Console.WriteLine("Stop entering 'ashley'");
}
}
while (!nameIsAshley);