How a subclass can invoke the base class constructor of its choice using the base keyword. - CSharp Custom Type

CSharp examples for Custom Type:Constructor

Description

How a subclass can invoke the base class constructor of its choice using the base keyword.

Demo Code

using System;//from   w  w  w  . ja  v a 2 s .  com
public class BaseClass
{
   public BaseClass()
   {
      Console.WriteLine("Constructing BaseClass (default)");
   }
   public BaseClass(int i)
   {
      Console.WriteLine("Constructing BaseClass({0})", i);
   }
}
public class SubClass : BaseClass
{
   public SubClass()
   {
      Console.WriteLine("Constructing SubClass (default)");
   }
   public SubClass(int i1, int i2) : base(i1)
   {
      Console.WriteLine("Constructing SubClass({0}, {1})", i1,  i2);
   }
}
public class Program
{
   public static void Main(string[] args)
   {
      SubClass sc1 = new SubClass();
      SubClass sc2 = new SubClass(1, 2);
   }
}

Result


Related Tutorials