Constructor

A constructor is a method that runs when a new instance of a class is created.
The job of the constructor is to create the object specified by a class and to put it into a valid state.
Constructors can be marked as public, private, protected, internal or protected internal.


A constructor is basically the class initialisation process
Two forms are supported
(1) inline field initializers
(2) constructors (constructor chaining also supported)


Instance and Static constructors supported
Static Constructors - called before types first use (also called type initializer)
Instance Constructors - called per-object allocation


A Copy Constructor creates a new object by copying variables from an existing object of the same type


Static Constructor

A static constructor is used to initialise any static fields, properties or events.
A static constructor can be used to perform an action that only needs to be performed once
It is called automatically before the first instance is created or any static members are referenced.
This allows you to control the initialization of static member data
Also referred to as a type initializer
A static constructor does not require any access modifier.
A class can only have one static constructor with no parameters.


public class MyClass 
{
   static MyClass()
   {
   }
}

Overloaded Instance Constructor

A class can have multiple instance constructors that can be overloaded
Having more than one constructor allows you to create the object with different parameters.
You must make sure each consructor has a unique signature.
The signature is composed of its name and its parameter list.

public class MyClass 
{
   public MyClass()
   {
   }
{
   public MyClass(string str)
   {
   }
}

Instance Constructor - Overloading


In C# we can supply parameters to constructors.
If a field is marked as read-only then the only place in which it can be assigned is in the constructor.




Base Constructor

This type of constructor will call BaseClass.BaseClass
If you omit the call to the base constructor it will be called automatically
If there is no default (parameteless) base constructor then one must be called explicitly
The constructor for the base class is called before the block for the constructor is executed

public class DerivedClass : BaseClass 
{
   public DerivedClass() : base()
   {
   }
}

This is the same as

public class DerivedClass : BaseClass 
{
   public DerivedClass()
}

You can also use this to pass items between the derived and the base classes
This type of constructor will call BaseClass.BaseClass(string)

public class DerivedClass : BaseClass 
{
   public DerivedClass(string message)
   {
      base(message);
   }
}

vs

public class DerivedClass : BaseClass 
{
   public DerivedClass(string message) : base(message)
   {
   }
}

The base keyword can be used with or without parameters
Use base when there is inheritance and a parent class already provides the same arguments



This Constructor

It is possible for a constructor to invoke another constructor in the same object.
Use this when you want to reference the current entity ??
The this keyword can be used with or without parameters

public class DerivedClass : BaseClass 
{
'//this constructor actualy calls the constructor below
   public DerivedClass(int value1, int value2) : this(value1 + value2)
   {
   }
   public DerivedClass(int value1)
   {
   }
}


Private Constructors

These are used to prevent creating instances of a class that do not have any instance fields or methods.
This is generally used in classes that contain only static members
It a class has one or more private constructors and no public constructor then it cannot be instantiated




© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext