developmentor - a developer services company				  
				      C# Tutorial
   Modules

Initialization


Goals:

  • Practice with variable initializers for instance fields.
  • Write instance constructors.
  • Have one instance constructor call another.

Overview

Initialization of data before use is relatively important for program correctness. C# offers three options for initializing fields and provides well defined rules for the execution order of the various options. The first initialization option for fields is to simply accept their default values. The numeric types get set to zero, Booleans to false, characters to the null character, and references to null. This case is relatively simple so we will not bother to test it in this exercise. The second initialization option is to assign an initial value at the point of definition. This case is also simple enough that we will not spend time testing it. The third initialization option is the use of constructors. This is the most complicated and powerful option and so will be the main topic covered in this activity.


part 1- Instance fields

Here we explore the two most interesting options for initializing an instance field: variable initializers and constructors. Initializing an instance field at the point of definition is called a "variable initializer". Constructors are initialization functions that get called automatically when an object is created.

steps:

  1. Here we will work with a Book class which represents a library book. We will store the title and author along with a flag to indicate whether the book is checked out. Implement a constructor for the Book class that takes three arguments: the title, author, and flag. To verify that your code is working correctly add a WriteLine statement to the constructor that prints the values being used for the initialization. To test your work, create a class called BookTest containing a Main method. In the Main method create a book object and pass arguments to the constructor.

    class Book
    {
    	private string title;
    	private string author;
    	private bool   available;
    	
    	public Book(string title, string author, bool available)
    	{
    		...
    	}
    	...
    }
    

  2. Add a second constructor to Book that takes only the title and author. Set the flag to true. Modify the Main method to create a Book using this new constructor.

    class Book
    {
    	public Book(string title, string author)
    	{
    		...
    	}
    	...
    }
    

  3. Add a third constructor to Book that takes only the title. Set the author to "anonymous" and the flag to true. Modify the Main method to create a Book using this new constructor.

    class Book
    {
    	public Book(string title)
    	{
    		...
    	}
    	...
    }
    

  4. Add a default (no argument) constructor to Book. Set the title to "untitled", the author to "anonymous", and the flag to true. Modify the Main method to create a Book using this new constructor.

    class Book
    {
    	public Book()
    	{
    		...
    	}
    	...
    }
    

  5. C# allows one constructor to call another constructor in the same class using a "constructor initializer". A constructor initializer can be used to eliminate repeated code. The most general constructor is fully implemented while the more specialized cases call the general one passing in whatever user specified data they have available and filling in the rest with hardcoded values. Rewrite the Book class constructors to take advantage of constructor initializers. The 3 argument constructor should be fully implemented and all other constructors should call it using the constructor initializer syntax.

    class Book
    {
    	public Book(string title, string author)
    		:this(title, author, true) // constructor initializer
    	{
    		...
    	}
    	...
    }
    

Solutions:

This material is excerpted from the Programming C# course offered by DevelopMentor.