#670 – Static vs. Instance Initialization

Static fields and properties are data members associated with a class itself, rather with instances of that class.  A static constructor can be used to initialize static members.  Static fields can also be initialized when they are declared.

The first time that you reference a static member, the following occurs:

  • All static fields are initialized
  • The static constructor is called
  • Your code, which accesses the static member, executes

If you reference an instance method, or create an instance of the class, before referencing any static members, the following occurs:

  • All static fields are initialized
  • The static constructor is called
  • All instance fields are initialized
  • The instance constructor is called
  • Your code, which accesses an instance member, executes

For example, initializing an instance of a Dog leads to the output shown below.

Dog d = new Dog();

Advertisements

#258 – Initializing a Static Variable as Part of Its Declaration

You can initialize a static variable at the time that it is declared.

        private static string LastDogToBark = "NOBODY";

The initialization of the field happens at runtime and is guaranteed to happen prior to the first time that the field is referenced or used.

#202 – All Fields in an Object Are Automatically Initialized

When you declare an instance of a value type without initializing it, the compiler prevents you from referencing the uninitialized variable.

            int x;

            Console.WriteLine(x);   // Compile-time error: [Use of unassigned local variable 'x']

If you declare and instantiate a reference type, the internal fields and properties are all initialized by setting all of the bits of the underlying memory for each item to 0.  This equates to:

  • Reference types = null
  • Numeric types = 0
  • Enum types = 0
  • Char type =
  • Boolean type = false

This means that value types declared inside the object are automatically initialized when the object is created.

For example, assume that we create a new Person object without calling a constructor that initializes any fields.

            Person p = new Person();

We can look at the new Person object in the debugger to see that all of its fields have been initialized.

#36 – Variable Initialization and Definite Assignment

Variables must be assigned a value before they can be used.  You can declare a variable without initializing it, but attempting to reference the value of the variable before you’ve given it a value will result in a compiler error.

 int i;
 int j = i + 1;   // Error: Use of unassigned local variable 'i'

This requirement of definite assignment before use means that you are never able to reference an unassigned variable in C#.

It’s considered good practice to initialize a variable when you declare it.  This applies to both built-in types and custom types.

When you initialize a variable, the object that it references is said to be instantiated.

 Person p = new Person();
 int i = 0;