The infamous and often unnecessary CS0165 compiler error

R

RayLopez99

Why? Why is the commented out first line below improper? The
overabundance of caution is the only thing I can figure. Not a big
deal, but strictly speaking unnecessary as the program should work
with this error.

RL

// char[] myCharacterArray; //gives: error CS0165: Use of unassigned
local variable

char[] myCharacterArray = new char[3]; //you must instantiate to avoid
this error
try
{
//do stuff here like...
char[] myCharacterArray = new char[3];
}
catch (Exception myexception)
{
}

foreach (char c in myCharacterArray)
{
Debug.WriteLine(c.ToString());
}
 
F

Family Tree Mike

RayLopez99 said:
Why? Why is the commented out first line below improper? The
overabundance of caution is the only thing I can figure. Not a big
deal, but strictly speaking unnecessary as the program should work
with this error.

RL

// char[] myCharacterArray; //gives: error CS0165: Use of unassigned
local variable

char[] myCharacterArray = new char[3]; //you must instantiate to avoid
this error
try
{
//do stuff here like...
char[] myCharacterArray = new char[3];
}
catch (Exception myexception)
{
}

foreach (char c in myCharacterArray)
{
Debug.WriteLine(c.ToString());
}
.

As an alternative, take the initialization line outside the try/catch. The
compiler does not assume that the variable successfully is initialized within
the try, leading to the error.

Mike
 
R

Registered User

Why? Why is the commented out first line below improper?
It's not that the one line itself is incorrect.
The overabundance of caution is the only thing I can figure.
Whose overabundance? ;)
Not a big
deal, but strictly speaking unnecessary as the program should work
with this error.
Comment out the try-catch wrapper. that specific error will go away
and the code will compile.
// char[] myCharacterArray; //gives: error CS0165: Use of unassigned
local variable

char[] myCharacterArray = new char[3]; //you must instantiate to avoid
this error
try
{
//do stuff here like...

The error happens because the instantiation of the array is done here
in the try block. If an exception occurs, this assignment may or may
not be made.
char[] myCharacterArray = new char[3];
}
catch (Exception myexception)
{
}

If that assignment isn't made due to an exception in the try block,
what will happen here?
foreach (char c in myCharacterArray)
{
Debug.WriteLine(c.ToString());
}

The compile time error is much preferable to the possibility of a run
time exception.

regards
A.G.
 
W

Willem van Rumpt

RayLopez99 said:
Why? Why is the commented out first line below improper? The
overabundance of caution is the only thing I can figure. Not a big
deal, but strictly speaking unnecessary as the program should work
with this error.

Because "myCharacterArray" is not definitely assigned. Eric Lippert (add
him to your must-read-blogs, it's always interesting *and* a good read!)
explains it in the following blog entry:

http://blogs.msdn.com/ericlippert/a...e-of-evidence-is-not-evidence-of-absence.aspx
 
Top