in C#, if i do something like:
int x;
then hit F10, I see that there's a zero stored in x.
but when i try to use x, it tells me that i should initialize it .. why is that ? there's supposed to be a zero there, right ?
the same thing is also true about arrays, so:
int[]a = new int[5];
if we F10 that, we'll see that all the ints in there are zeros..
What's going on ? and why should i ever MUST initialize a variable in C# unlike in C++ ?
It's a compile time error trying to help you reduce bugs in your code due to un-initialized variables. Very useful.
http://msdn.microsoft.com/en-us/library/4y7h161d(VS.80).aspx
More reading:
http://blogs.msdn.com/b/abhinaba/archive/2005/11/08/490248.aspx
Some words on disabling the error (you can't just to warn you - it's not a warning):
http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/a073ccea-c43b-4536-be76-41949f0e2135
and
http://blogs.msdn.com/b/ericlippert/archive/2009/10/12/absence-of-evidence-is-not-evidence-of-absence.aspx (proves to be most informative on the subject)
and even more discussing relationship between "localsinit" IL flag (that forces default values to be set for locals) and its relation to "double assignment" (C# forces you to write int v=0
even if it is already there). And if you want to read even deeper - "localsinit" defined in section I.12.2 of ECMA-335 available here.
Simply put, because that's the way C# works. From the C# Language Specification:
5.3.2 Initially unassigned variables
The following categories of variables are classified as initially unassigned:
· Instance variables of initially unassigned struct variables.
· Output parameters, including the this variable of struct instance constructors.
· Local variables, except those declared in a catch clause or a foreach statement.
On the other hand:
5.3.1 Initially assigned variables
The following categories of variables are classified as initially assigned:
· Static variables.
· Instance variables of class instances.
· Instance variables of initially assigned struct variables.
· Array elements.
· Value parameters.
· Reference parameters.
· Variables declared in a catch clause or a foreach statement.
Simple... because the compiler insists that you initialize local variables before using them. This prevents a whole category of mistakes associated with failure to initialize.
If you look at the IL generated from the following few statements:
int x;
Console.WriteLine("hello");
int y=5;
x=6;
Console.WriteLine(x+y);
you see the following:
//notice no ops related to x prior to console.writeline
IL_0000: ldstr "hello"
IL_0005: call System.Console.WriteLine
IL_000A: ldc.i4.5
IL_000B: stloc.1 //create/store y
IL_000C: ldc.i4.6
IL_000D: stloc.0 //x is really only created right here
IL_000E: ldloc.0
IL_000F: ldloc.1
IL_0010: add
IL_0011: call System.Console.WriteLine
if you're seeing a value for x prior to it's storing in IL, then it's a debugger trick.
The auto-initialization is a runtime security feature I believe, not a language guarantee. It happens that .NET zeroes variables, but that doesn't have to be true with every framework.
Note that for arrays, it is guaranteed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With