C# variable initializations vs assignment

跟風遠走 提交于 2019-11-29 12:48:02

If all you are asking about is terminology (*which isn't really clear from your question), "Initialization" of a variable is, literally, the first time a value is assigned to it. This term comes from the fact that you are giving the variable it's "initial" value.

This should (obviously) happen before it is used the first time.

int x=5;

is a declaration and an initialization, and is really just convenient shorthand for

int x;
x=5;

Generally, initialization of a variable is the first assignment after declaration, as variables are not automatically initialized. Subsequent assignments are just assignments.

void foo() {
    int i;//not initialized.
}

Except for fields, which are variables declared in a class or struct. These are initialized to their default values just before the constructor is called for that object instance. Hence, even if you (first) assign something to a field in a constructor and that field was not initialized at declaration, it is strictly speaking assignment, not initialization. Without going into such detail, I guess many programmers do consider the first assignment of a field in a constructor as initialization though, and may use it as such in their conversations with each other. Probably this usage/habit stems from having used unmanaged languages like C/C++ where a variable contains gibberish when first declared, and hence they have to fill it with non-rubbish values (initialization).

class A {
    int x;//not initialized
    int y = 1;//initialized here
    A() {
        x = 1;//strictly speaking, not initialization, just assignment.
        y = 2;//was obviously initialized above
    }
}

For programmers not writing compilers or languages, to be able to communicate effectively with other programmers is more important than knowing the exact terminology of the word initialization. So go ahead and use it in the meaning which everyone around you understand. You will not be wrong to explain the exact meaning though (but I probably won't for fear of exposing my pedantic tendencies).

Personally I will avoid a book that uses a term "initialization command" for variables.

In general, initialization if the first time a variable is assigned to, whether explicitly by the program or implicitly by the compiler/run-time. However the mechanisms of initialization are important to understand especially when working with a classes. Consider stepping through the simple example below (Create an instance of 'Bar' somewhere) to see how the compiler goes about the process of initialization. You will notice that the fields are declared with initialization and all are "initialized" before the constructors have a chance to set them.

public class barBaseBase
{
    protected int x = 5;
    public barBaseBase()
    {
        x = 4;
    }
}

public class barBase : barBaseBase
{
    protected int y = 4;
    public barBase()
    {
        x = 3;
        y = 2;
    }
}

public class Bar : barBase
{
    protected int z = 3;
    public Bar()
    {
        x = 2;
        y = 1;
        z = 0;
    }
}

Note that initializing fields causes the C# compiler to emit the initialization IL instructions for each constructor. So if you have multiple constructors, you'll have duplication of the initialization IL - i.e. unnecessary code bloat.

Better to initialize the variables in the default constructor and have the other constructors call that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!