The reason is simple: the system can see a possible route through the code that could result in a variable being used (i.e. read or checked) before you actually assign a value to it. That the route is unlikely, or even not actually possible in practice is irrelevant to the compiler - it doesn't "know" what is going to be in the other variables such as user input.
For example:
string s = "123";
bool b;
foreach (char c in s)
{
if (c != 'a') b = true;
}
if (b) Console.WriteLine("YES");
In practice,
b
will always be
true
after the loop - but the system can't rely on that and tells you off because you could easily change it to this:
string s = Console.ReadLine();
bool b;
foreach (char c in s)
{
if (c != 'a') b = true;
}
if (b) Console.WriteLine("YES");
And if the user just presses ENTER, then
b
is never given a value.
So always give variables a default value:
bool bolValidLength = false;
and you won't have a problem.