By 22/07/2015 5 Comments

C# Inheritance Constructor Calling Order

  • In C# inheritance if our both classes (base and derived) has constructors then that constructors are called through derivation order from base to derived class OR in inheritance constructors are called from base to derived class order.
  • If no constructor is defined in base class then its NULL parameterized or default constructor will be executed.

Example:

using System;
namespace csharpBasic
{
    class MarkSheet
    {
        // MarkSheet constructor.
        public MarkSheet()
        {
            Console.WriteLine("Constructing MarkSheet...");
        }
    }

    class BcsMarkSheet : MarkSheet
    {
        // BcsMarkSheet constructor.
        public BcsMarkSheet()
        {
            Console.WriteLine("Constructing BcsMarkSheet...");
        }
    }

    class SoftwareEngMarksheet : BcsMarkSheet
    {
        // SoftwareEngMarksheet constructor.
        public SoftwareEngMarksheet()
        {
            Console.WriteLine("Constructing SoftwareEngMarksheet...");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Object creation of derived SoftwareEngMarksheet through derived class default constructor.
            SoftwareEngMarksheet softwareEngMarksheet = new SoftwareEngMarksheet();
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Constructing MarkSheet...
        Constructing BcsMarkSheet...
        Constructing SoftwareEngMarksheet...  
         
       */
    }
}






Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .