C# Tutorial - More on Classes

posted Jun 26, 2011, 11:13 PM by magic stone

Every class has the same defining layout:

[access-modifier] class [identifier] [:base-class]


  • The access-modifier is optional. If you do not define one, then the method is defaulted to public, because a class cannot be private.

  • The keyword class identifies the type of the object.

  • The [identifier] is the name of the class being declared.

  • The [:base-class] tag allows a class to inherit from another class. Inheritance is discussed later.

  • The class-body contains all of the code that is associated with an object.


When a class is declared as in the following code, we only say that we want to have an instance of the class in our program. We have not yet created the class in program memory.

Person Paul;

To do this we must instantiate an instance of the class, this is done using the new keyword, as in the following code:

Person Paul = new Person();

You can see that after the new keyword we called a method with the same name as the class. This is called its constructor. By calling this constructor, memory is allocated for the class, and we can now use it in our program, as in the next code sample.

Person Paul = new Person();
String surname = "Smith";
Paul.setSurname( surname );

A constructor is a method that is invoked upon instantiation of a class. In C# these are known as "instance constructors". An instance constructor is a member that implements the actions required to initialize an instance of a class. A constructor is invoked when you use the new operator, or use the various methods of reflection to create an instance of a class.

Defining the Constructor

Every constructor has the same defining layout:

[access-modifier] constructor_name (parameters)
  // constructor body


  • The [access-modifier] can be privatepublicprotected or internal.

  • The constructor_name of a constructor must be the same as the name of the class.

A constructor can take zero or more arguments as parameters. A constructor with zero arguments (that is no-arguments) is known as a default constructor. A class can have more than one constructor, by giving each constructor a different set of parameters. This gives the user the ability to initialise the object in more than one way.

The constructor body can be used to initialise the object into a known starting state.

Writing the Constructor

In, general, a constructor’s access modifier is public, this is because the class that is creating an instance of this class, needs to be able to access the constructor of the class.

public Person()
  . . .


Each class also requires a destructor, this is the code that is called to clean up the object when it has been finished with. However, if you do not provide one, then the Garbage Collector will deal with freeing up the memory for you. Any object that you create in the class, should be deleted from the class as a general rule of thumb.

A destructor is defined pretty much in the same way as a constructor, but has a tilde (~) in front of the method name.

public ~Person()
  . . .

Everything Else

Apart from the constructor and destructor, the class body can contain as many attributes and methods as required. However, if a class is getting too large to work with, think about logically splitting it into smaller classes, or into smaller files using partialclasses.