简体   繁体   中英

Noninitialized variable in C#

I have the following piece of code:

class Foo
{

    public Foo()
    {
        Bar bar;
        if (null == bar)
        {

        }
    }
}

class Bar { }

Code gurus will already see that this gives an error. Bar might not be initialized before the if statement.

What is the value of bar? Shouldn't it be null? Aren't they set to null? (null pointer?)

No, local variables don't have a default value 1 . They have to be definitely assigned before you read them. This reduces the chance of you using a variable you think you've given a sensible value to, when actually it's got some default value. This can't be done for instance or static variables because you don't know in what order methods will be called.

See section 5.3 of the C# 3.0 spec for more details of definite assignment.

Note that this has nothing to do with this being a reference type variable. This will fail to compile in the same way:

int i;
if (i == 0) // Nope, i isn't definitely assigned
{
}

1 As far as the language is concerned, anyway... clearly the storage location in memory has something in it, but it's irrelevant and implementation-specific. There is one way you can find out what that value is, by creating a method with an out parameter but then using IL to look at the value of that parameter within the method, without having given it another value. The CLR doesn't mind that at all. You can then call that method passing in a not-definitely-assigned variable, and lo and behold you can detect the value - which is likely to be the "all zeroes" value basically.

I suspect that the CLI specification does enforce local variables having a default value - but I'd have to check. Unless you're doing evil things like the above, it shouldn't matter to you in C#.

Fields (variables on classes / structs) are initialized to null /zero/etc. Local variables... well - since (by "definite assignment") you can't access them without assigning there is no sensible way of answering; simply, it isn't defined since it is impossible. I believe they happen to be null /zero/etc (provable by hacking some out code via dynamic IL generation), but that is an implementation detail.


For info, here's some crafy code that shows the value of a formally uninitialised variable:

using System;
using System.Reflection.Emit;
static class Program
{
    delegate void Evil<T>(out T value);
    static void Main()
    {
        MakeTheStackFilthy();
        Test();
    }
    static void Test()
    {
        int i;
        DynamicMethod mthd = new DynamicMethod("Evil", null, new Type[] { typeof(int).MakeByRefType()});
        mthd.GetILGenerator().Emit(OpCodes.Ret); // just return; no assignments
        Evil<int> evil = (Evil<int>)mthd.CreateDelegate(typeof(Evil<int>));
        evil(out i);
        Console.WriteLine(i);
    }
    static void MakeTheStackFilthy()
    {
        DateTime foo = new DateTime();
        Bar(ref foo);
        Console.WriteLine(foo);
    }
    static void Bar(ref DateTime foo)
    {
        foo = foo.AddDays(1);
    }
}

The IL just does a "ret" - it never assigns anything.

Local variables do not get assigned a default value. You have to initialize them before you use them. You can explicityly initialize to null though:

public Foo()
{
    Bar bar = null;
    if (null == bar)
    {

    }
}

局部变量没有分配默认值,甚至没有null

No, local variables are not automatically set to 0 (default).

But because you (always) get that error, it really doesn't matter. If it had a another value the compiler would never let you find out.

Not to be confused with field variables (class members), they are initialized to their type's default value (0/null/false/...).

The value of bar is undefined. There's space allocated for it on the stack, but the space isn't initialised to any value so it contains anything that happened to be there before.

(The local variable might however be optimised to use a register instead of stack space, but it's still undefined.)

The compiler won't let you use the undefined value, it has to be able to determine that the variable is initialised before you can use it.

As a comparison, VB does initialise local variables. While this can be practical sometimes, it can also mean that you unintenionally use a variable before you have given it a meaningful value, and the compiler can't determine if it's what you indended to do or not.

It doesn't matter because no such code should be compilable by any compiler that implements C#.

If there was a default value, then it would be compilable. But there is none for local variables.

Besides "correctness", local variable initialization is also related to the CLR's verification process .

For more details, see my answer to this similar question: Why must local variables have initial values?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM