Reputation: 369
So, I'm beginner in C# and I really don't know why I'm getting "Use of unassigned local variable error" for the variable "name". I have this simple code that asks for a name, and if it's not Bob or Alice, it shows a message.
using System;
namespace exercise2
{
class Program
{
static void Main(string[] args)
{
string name;
int i = 0;
while (i == 0)
{
Console.Write("What is your name?\n>>> ");
name = Console.ReadLine();
if ((name == "Alice") || (name == "Bob"))
{
i = 1;
Console.Clear();
}
else
{
Console.WriteLine("You're not Alice or Bob.");
Console.ReadKey();
i = 0;
Console.Clear();
}
}
Console.WriteLine("Good Morning, " + name); //"name" is unassigned
Console.ReadKey();
}
}
}
Hope that it's not a stupid question.
Thanks
Upvotes: 2
Views: 1403
Reputation: 115538
The name variable is unassigned at some point in the branching process where it could be used. You could assign it a default value, but refactoring it is a better solution.
Move the name variable inside the while loop to avoid reassignment. (putting it inside the while loop technically isn't reassignment of the same variable, because when it loops again, a new variable is created and the previous set value is unavailable).
Move the following two lines into the true portion of the conditional:
Console.WriteLine("Good Morning, " + name);
Console.ReadKey();
static void Main(string[] args)
{
int i = 0; //this should really be bool,
//because all you're doing is using 0 for repeat and 1 for stop.
while (i == 0)
{
Console.Write("What is your name?\n>>> ");
string name = Console.ReadLine();
if ((name == "Alice") || (name == "Bob"))
{
i = 1;
Console.Clear();
Console.WriteLine("Good Morning, " + name); //"name" is unassigned
Console.ReadKey();
}
else
{
Console.WriteLine("You're not Alice or Bob.");
Console.ReadKey();
i = 0;
Console.Clear();
}
}
}
}
Upvotes: 1
Reputation: 30813
This is because compiler cannot "see" the evaluation on while()
statement will definitely be true
and name
will be assigned in the while
block for the first time.
Change your
string name;
into
string name = ""; //or string.Empty
Although as human we can read easily that the while block is going to be executed for the first time:
string name; //unassigned
int i = 0;
while (i == 0) //will enter
{
Console.Write("What is your name?\n>>> ");
name = Console.ReadLine(); //name will definitely be assigned here
... something else
}
Console.WriteLine("Good Morning, " + name); //compiler cannot "see" that the while loop will definitely be entered and name will be assigned. Therefore, "name" is unassigned
The compiler cannot see that, thus giving you the error.
Alternatively, you could also change the while
block to do-while
to force the compiler to see that the name
will be assigned (Credit goes to Lee):
string name; //unassigned
int i = 0;
do //will enter
{
Console.Write("What is your name?\n>>> ");
name = Console.ReadLine(); //name will definitely be assigned here
... something else
} while (i == 0);
Console.WriteLine("Good Morning, " + name); //should be OK now
Upvotes: 3