Declaration, Instance, Initialization, and Assignment Values

Technically, what are the meanings and differences of terms that declare, instantiate, initialize, and assign an object in C #?

I think I know the meaning of the appointment, but I do not have a formal definition.

Msdn says that the "action to create an object is called an instance." But the meaning of creation seems vague to me. You can write

int a; 

is there a then created?

+10
c # terminology
Aug 29 '15 at 21:34
source share
2 answers

Announcement . Declaring a variable means introducing a new variable into the program. You determine its type and its name.

 int a; //a is declared 

Instantiate Creating an instance of a class means creating a new instance of the class. Source

 MyObject x = new MyObject(); //we are making a new instance of the class MyObject 

Initialize . To initialize a variable, you need to assign it an initial value.

 int a; //a is declared int a = 0; //a is declared AND initialized to 0 MyObject x = new MyObject(); //x is declared and initialized with an instance of MyObject 

Destination . Assigning a variable means providing a variable with a value.

 int a = 0; //we are assigning to a; yes, initializing a variable means you assign it a value, so they do overlap! a = 1; //we are assigning to a 
+18
Aug 29 '15 at 21:55
source share

Generally:

To declare means to inform the compiler that something exists, so space can be allocated for it. This is separate from defining or initializing something in that it does not necessarily say what the β€œvalue” of a thing is, only that it exists. In C / C ++, there is a strong difference between declaration and definition. There is a much smaller difference in C #, although the terms can still be used in a similar way.

Instantiate literally means create an instance. In programming, this usually means creating an instance of the object (usually on the heap). This is done using the new keyword in most languages. i.e.: new object(); . Most of the time you will also save the link to the object. i.e.: object myObject = new object(); .

Initializing means giving an initial value. In some languages, if you do not initialize a variable, it will contain arbitrary (dirty / garbage) data. In C #, this is actually a compile-time error for reading from an uninitialized variable.

The purpose is simply to save one value of a variable. x = 5 assigns the value 5 variable x . In some languages, an assignment cannot be combined with a declaration, but in C # it can be: int x = 5; .

Note that the operator object myObject = new object(); combines all four of them.

  • new object() creates a new object object , returning a reference to it.
  • object myObject declares a new object link.
  • = initializes the reference variable, assigning it the value of the link.
+5
Aug 29 '15 at 22:07
source share



All Articles