C# variable initializations vs assignment
In a book I found following (translation):
Initialization means assigni开发者_如何学JAVAng the value of the variable at the declaration time.
int X=5
is called an initialization command.
EDIT: It just says that term initialization is used only when you assign value at the declaration time. If you do it later, its just assignement (according to it - I do not think so and that is why I am asking). Is it true or not?
Well, I have always thought (and according to others on the net, too) about initialization in the terms of first assignment of a value to a variable. I think that int X=5
is just assignment as a part of declaration.
I tried to search on MSDN with no luck. Thanks for any information.
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.
精彩评论