In C #, why do I see there the zero stored in my int variable when I declare it, but still get an error message that I have to initialize?

in C #, if I do something like:

int x; 

then press F10, I see that there zero is stored in x.

but when I try to use x, it tells me that I should initialize it. Why is this? there must be zero, right?

The same applies to arrays, therefore:

 int[]a = new int[5]; 

if we F10 that, we will see that all ints there are zeros there ..

What's happening? and why should I ever MUST initialize a variable in C #, unlike C ++?

+8
variables c #
source share
5 answers

This is a compile-time error that will help you reduce the number of errors in your code due to uninitialized variables. Very useful.

http://msdn.microsoft.com/en-us/library/4y7h161d(VS.80).aspx

Additional Information:

http://blogs.msdn.com/b/abhinaba/archive/2005/11/08/490248.aspx

A few words about disabling the error (you cannot just warn you - this is 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 (it turns out to be the most informative on this issue)

and even discuss the relationship between the IL localsinit IL flag (which sets default values ​​for local users) and its relation to "double assignment" (C # forces you to write int v=0 , even if it already exists). And if you want to read even deeper - "localsinit", defined in section I.12.2 of ECMA-335, available here .

+7
source share

Simply put, because it works with C #. 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 structural variables.

. Output options, including this struct instance constructor variable.

. Local variables, except those declared in a catch or foreach clause.

On the other hand:

5.3.1 Initially assigned variables

The following categories of variables are classified as originally assigned:

. Static Variables

. Instance variables of a class.

. Instance variables of the originally assigned structure variables.

. Array elements

. Value Parameters.

. Reference parameters.

. Variables declared in a catch or foreach clause.

+4
source share

Simple ... because the compiler insists that you initialize local variables before using them. This prevents a whole category of errors related to initialization failure.

If you look at IL generated from the following few statements:

 int x; Console.WriteLine("hello"); int y=5; x=6; Console.WriteLine(x+y); 

You will 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 see the value for x before storing it in IL, then this is a debugger trick.

+3
source share

This 0 is just luck, it can be anything, so you must initialize it.

+2
source share

Automatic initialization is a safety feature at runtime, I suppose, not a language guarantee. It happens that .NET nullifies variables, but this does not have to be true in every infrastructure.

Please note that for arrays is guaranteed .

+2
source share

All Articles