Vb.net Question

What is the difference between "instantiated" and "initialized"?

I've been hearing these two words used in Microsoft tutorials for VB.NET. What is the difference between these two words when used in reference to variables?


Value vis-a-vis Reference Types

Variables in C# are in 1 of 2 groups. Value types or Reference types. Types like int and DateTime are value types. In contrast, any class you create is a reference type. C# strings are also a reference type. Most things in the .NET framework are reference types.

Parts of a Variable

There is the variable name and it's value. Two parts.

The variable's name is what you declare it to be. The value is what you assign to it.

Variables are Initialized

All variables are always given an initial value at the point the variable is declared. Thus all variables are initialized.

For value types, like int the compiler will give them a valid value if you do not do so explicitly. int's initialize to zero by default, DateTime's initialize to DateTime.MinValue by default.

Reference type variables initialize to the object you give it. The compiler will not assign an object (i.e. a valid value) if you don't. In this case the value is null - nothing. So we say that the reference is initialized to null.

Objects are Instantiated

Humans are born. Objects are instantiated. A baby is an instance of a Human, an object is an instance of some Class.

The act of creating an instance of a Class is called instantiation (Ta-Da!)

So declare, initialize, and instantiate come together like this

MyClass myClassyReference= new MyClass();

In the above, it is wrong to say "... creating an instance of an object..."

The Unbearable Lightness of Being

A reference type variable's name and value exists independently. And I do mean independent.

An instantiated object may or may not have a reference to it.

An instantiated object may have many references to it.

A declared reference may or may not be pointing to an object.