I've noticed that C# compiler (.NET 4.5.2) doesn't allow me to compile the following code:
public void Test(out string value)
{
//value = null;
try
{
value = null;
}
catch (Exception ex)
{
//value = null;
}
}
It fails with the following error:
The out parameter 'value' must be assigned to before control leaves the current method
But if I'm uncommenting the assignment in the catch
section, it compiles successfully.
Obviously, it also compiles when I'm uncommenting the assignment before the try
statement.
So the question is why it's insufficient to have the initialization of an out
parameter inside a try block? Why am I forced to do the initialization in the catch
block as well?
null
can be stored in the variable, it has to be pushed onto the stack with aldnull
. There is no reason that couldn't cause aStackOverflowException
or something. – Dearly