C # Confusion initialization!
int? test;
try
{
test = (int?) Int32.Parse ("7");
} catch {}
if (test == null)
Console.WriteLine("test is null!");
else
Console.WriteLine("test = {0}", test);
I have some code that does something VERY similar to this, same idea ... Create a variable, try to initialize it, and then check if the initialization was successful.
Visual Studios gives me the error "Using unassigned local variable 'test', which is quite annoying, it's easy to fix by setting the first line:
int? test = null;
but I'm curious what the difference is between these two lines, because the compiler really cares. And as far as I know, the two lines do the same.
a source to share
The problem is the catch blocking. The compiler must assume that the Int32.Parse code can throw and therefore end up in your catch block. In case the Int32.Parse string is not terminated and therefore never assigned a value for the test. This means that the "if" line is trying to use an uninitialized value.
You can fix this with
- Assigning a test to a value in a catch block
- Initializing it to zero at the beginning of the method
a source to share
you are confusing the difference between what is variable declaration and what is variable initialization
int? test;
simply states that you have a variable named test which is a nullable int
but
int? test = null;
indicates that you have a variable named test that is nullable int and its value is null
There is no difference in VB, but there is a difference in C #. This is why the compiler complains, because if something fails in your try block, your test variable would never be initialized.
a source to share
This is a bug that always occurs for locally defined variables (inside a method or property, not directly inside a class). While the fact remains that the compiler does not have to generate this error in order to work, it does so specifically to help you identify potential unexpected results in the case of not always assigning variables. (Someone will correct me if I'm wrong, but at least some previous versions of the C # compiler did not check for unassigned variables in some / all cases.)
Equivalently (instead of being assigned test = null
in a declaration), you can fix the error by setting test = null
in a catch block, as this will mean that any path the code takes is assigned to a variable test
. However, I think that the resolution you specified (by assigning null in your declaration) is correct - you will see it very often in C # code that is very much promoted (via try-catch commands, if statements or whatever else) - and honestly, it helps you figure out what and when you are assigning your variables, even if it might seem like a minor annoyance.
a source to share
I find this style is much cleaner:
try
{
int test = Int32.Parse("7");
Console.WriteLine("Success!");
// add return statement for successful execution here, if any
}
catch
{
Console.WriteLine("Disaster!");
// other return statement here, if any
}
As far as compiler error goes: any local field must be explicitly initialized on the code path before reading it. A common mistake is not initializing local fields, so this is a bug in C #. C / C ++ only warns about this, and it can give * funny * results if it is not initialized and the value reflects the bytes that were already on the call stack.
I can only guess this, but it might be a performance aspect that explicitly initializes local fields as opposed to class fields. When an object is instantiated, the object's memory stream is less initialized for runtime, however, initializing the local field multiple times with each method call is redundant.
a source to share