Meanings of declaring, instantiating, initializing and assigning an object

Declaring - Declaring a variable means to introduce a new variable to the program. You define its type and its name.

int a; //a is declared

Instantiate - Instantiating a class means to create 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 means 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

Assigning - Assigning to a variable means to provide the 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

In general:

Declare means to tell the compiler that something exists, so that space may be allocated for it. This is separate from defining or initializing something in that it does not necessarily say what "value" the thing has, only that it exists. In C/C++ there is a strong distinction between declaring and defining. In C# there is much less of a distinction, though the terms can still be used similarly.

Instantiate literally means "to create an instance of". In programming, this generally means to create an instance of an object (generally on "the heap"). This is done via the new keyword in most languages. ie: new object();. Most of the time you will also save a reference to the object. ie: object myObject = new object();.

Initialize means to give an initial value to. In some languages, if you don't initialize a variable it will have arbitrary (dirty/garbage) data in it. In C# it is actually a compile-time error to read from an uninitialized variable.

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

Note that the statement object myObject = new object(); combines all four of these.

  • new object() instantiates a new object object, returning a reference to it.
  • object myObject declares a new object reference.
  • = initializes the reference variable by assigning the value of the reference to it.