Instance Constructors - CSharp Custom Type

CSharp examples for Custom Type:Constructor

Introduction

Constructors run initialization code on a class or struct.

A constructor is defined like a method, except that the method name and return type are reduced to the name of the enclosing type:

public class Animal
{
  string name;                   // Define field
  public Animal (string n)        // Define constructor
  {
    name = n;                    // Initialization code (set up field)
  }
}

Animal p = new Animal ("Petey");   // Call constructor

Instance constructors allow the following modifiers:

Modifier Value
Access modifiers public internal private protected
Unmanaged code modifiers unsafe extern

Related Tutorials