Lecture overview -- Keyboard shortcut: 'u'  Previous page: Instantiation of classes in C# -- Keyboard shortcut: 'p'  Next page: Constructors in C# -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 26 : 29
Object-oriented Programming in C#
Classes and Objects
Initialization of objects

The step after instantiation is called initialization. Without initialization, the memory of the allocated object may contain random values. In this context, random values is the same as garbage! In order to avoid this, a new object should always be initialized. In C#, constructors are responsible for initialization of the instance variables.

Initialization is the process of ascribing initial values to the instance variables of an object

  • Initialization of objects of type T

    • Via use of default values of T

      • zero for numeric types, false for bool, '\x0000' for char, and null for reference types

    • Via use of an initializer

    • Via special methods called constructors

Do not rely on default values!

It is recommended that you bundle all initializations of instance variables in constructors.

It is very important that a newly born object is initialized to a healthy citizen in the population of objects

Explicit initialization is always preferred over implicit initialization

Always initialize instance variables in constructors

Read more about initialization in the text book version of this material.