By 22/07/2015 1 Comments

C# Base Constructor

  • In C# inheritance if the both classes (base and derived) have their own constructors than the constructor of base class constructs/create the base class object and derived class constructor creates/constructs the derived class object.
  • If base and derived class does not contain any explicit constructor than the default constructor will be called automatically in both classes (base and derived).
  • If a constructor is defined in only a derived class than it creates/constructs the object of derived class and a default construct is used in base class which constructs a base class object automatically (example defined in preceding program).
  • When both classes (base and derived) define their own constructors explicitly than these both base and derived class constructors must be executed, in that condition a C#’s keyword “base” must be used. The keyword “base” is used to access or call a base class constructor in derived class and it also access base class members in derived class which is hidden by derived class’s members.
  • A base class constructor can be called by a derived class constructor in its derived class through “base” keyword.

General Syntax:

DerivedClassConstructor (parameterList): base (parameterList)
constructor body

DerivedClassConstructor (parameterList) This can be derived class constructor with optional parameter list.
: base (parameterList) The keyword “base” is used which calls base constructor by using (parameterList), if this parameter list matches with any base class’s constructor parameter list than that constructor of base class will be executed/called in its derived class.
{ constructor body } Here can be any code / logic.


using System;
namespace csharpBasic
    // A base class declaration.
    class MarkSheet
        // Fields declaration with protected access.
        protected double AspMarks;
        protected double CSharpMarks;

        // Parametrized constructor which sets marks for each subject.
        public MarkSheet(double aspMarks, double cSharpMarks)
            this.AspMarks = aspMarks;
            this.CSharpMarks = cSharpMarks;

        // Double type empty parametrized method.
        public virtual double GetObtainMarks()
            return this.AspMarks + this.CSharpMarks;

        // Double type 2 parameterized method.
        public double GetPercentage(double obtainMarks, double totalMarks)
            return (obtainMarks / totalMarks) * 100.0;

    // Derived class declaration.
    class BcsMarkSheet : MarkSheet
        double PhpMarks;

        /* Parametrized constructor which sets marks for each subject in derived class 
           and also called a base class consturctor.*/
        public BcsMarkSheet(double aspMarks, double cSharpMarks, int phpMarks)
            : base(aspMarks, cSharpMarks)
            this.PhpMarks = phpMarks;

        // Double type methods.
        public double GetAspMarks()
            return this.AspMarks;

        public double GetCsharpMarks()
            return this.CSharpMarks;

        public double GetPhpMarks()
            return this.PhpMarks;

        public override double GetObtainMarks()
            return this.AspMarks + this.CSharpMarks + this.PhpMarks;

    class Program
        static void Main(string[] args)
            // Derived class marksheet is being constructed.
            BcsMarkSheet bcsMarkSheet = new BcsMarkSheet(85, 70, 60);
            // Different types of marksheet information is displayed.
            Console.WriteLine("Bcs Mark sheet information: ");
            Console.WriteLine("Subject 1:");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetAspMarks());
            Console.WriteLine("Subject 2: Csharp");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetCsharpMarks());
            Console.WriteLine("Subject 3: Php");
            Console.WriteLine("Marks: {0}", bcsMarkSheet.GetPhpMarks());
            Console.WriteLine("Obtain marks: {0}", bcsMarkSheet.GetObtainMarks());
            Console.WriteLine("Percentage: {0}", bcsMarkSheet.GetPercentage(bcsMarkSheet.GetObtainMarks(), 300));
        The Output will be:
        Bcs Mark sheet information:
        Subject 1:
        Marks: 85
        Subject 2: Csharp
        Marks: 70
        Subject 3: Php
        Marks: 60
        Obtain marks: 215
        Percentage: 71.6666666666667

Next Tutorial →

← Previous Tutorial

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 .