The aim of this code is to repeat the question “Please enter your name” if the user does not input any data. However I am having trouble with making this work with the if statement.
while (true)
{
Console.WriteLine("Please enter your name:");
string line = Console.ReadLine();
if (line=String.empty) //I'm having difficulty making this a valid statement
Console.WriteLine("Your entry was blank");
else break;
}
As looking your code, the error is in using making condition….You are using
assignment operator(=)instead ofcomparision operator(==)……So do like this :Or,You can simply do like this:
Or, you can use
string.IsNullOrWhitespaceas Oded answer specified but it is only available in.NET 4or above…..