28

Technically what are the meanings and differences of the terms declaring, instantiating, initializing and assigning an object in C#?

I think I know the meaning of assigning but I have no formal definition.

In msdn, it is said "the act of creating an object is called instantiation". But the meaning creating seems vague to me. You can write

int a;

is a then created?

8
  • i have never seen clarifying. but instantiating means to create an instance of an object. initializing means setting value to an object.(does not necessarily create new instance). assigning is self descriptive. assign a value to an object. i maybe wrong. but thats what i think Aug 29, 2015 at 21:42
  • FYI, int is a primitive type, not an object. Aug 29, 2015 at 21:45
  • sorry I meant declaring. Aug 29, 2015 at 21:45
  • declaring is just what you have shown. like int a; you declared an int named a but you still not initialized it. Aug 29, 2015 at 21:52
  • 1
    instantiate is for class objects. means create a new object with new reference. for a=2 you say initialize. Aug 29, 2015 at 21:57

2 Answers 2

59

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
3
  • So Instantiation is not used for structs or simple types? Aug 29, 2015 at 22:00
  • Can "initializing" mean "assigning for the first time after declaration"? Aug 29, 2015 at 22:04
  • 1
    The term is used for structs, although I am honestly not sure why. However "instantiate" is not used for simple types. And yes, that is basically the meaning of initialize. @MinimusHeximus
    – Zarwan
    Aug 29, 2015 at 22:10
11

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.
2
  • Do we have the term defining too? What defining means? Aug 29, 2015 at 22:22
  • 1
    @MinimusHeximus Define means give a value (or body) to. We don't really use "define" much in C#, but it would be similar in meaning to initialize. When you write a method, I guess you could call that defining the method, though it doesn't have any contrast with declaring the method like it would in C++. Aug 29, 2015 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.