By 22/07/2015 8 Comments

C# Base Keyword

  • In C# if we do not want to use base keyword than we must define a NULL parameter constructor in base/father class explicitly along with different parametrized constructors because in inheritance always NULL parameter constructor is executed in base/father class by default, so we must use a base keyword to prevent a NULL constructor of base/father class to be executed at every time or we should not define a NULL parametrized constructor in base/father class explicitly and use base keyword.
  • If base and derived class defines their own constructors with different parameters than the base keyword must be specified in derived class which calls a base class constructor (depends on parameters passed by base keyword) in its derived class automatically OR When we call a derived class constructor that derived class constructor calls/invoke automatically to base class constructor through base keyword.
  • Same base class members can be re-declared in the derived class with same names, in this situation the members of the base class is being hidden in derived class.
  • If you want to hide a base class member in its derived class than that members must be marked as new members through a keyword new, this new keyword is different from other “new” keyword which is used to create object of the class. The “new” keyword defines its own new members and hides the base class members in derived class.
  • Hidden members of the base class can be accessed/uncovered in derived class by using keyword base.
  • The base keyword works like “this” keyword but base refers to the base class and its members (methods, fields etc.) in its derived class, such as (base.member) where member can be any base class member such as fields and methods.
  • The base keyword is useful for accessing/uncovers the hidden members of base class in its derived class.

General Syntax:

base.member;

STATEMENT DESCRIPTION
base.member; Where base is a keyword and member can be method or instance variable/field in a derived class.

Example – 1, Constructors with base keyword:

using System;
namespace csharpBasic
{
    // A base class declaration.
    class TestBaseConstructor
    {
        // 3 overloaded constructors are defined as following.
        public TestBaseConstructor(int x, int y)
        {
            Console.WriteLine("Inside base class 2 parameterized constructor.");
        }

        public TestBaseConstructor(int x)
        {
            Console.WriteLine("Inside base class 1 parameterized constructor.");
        }

        public TestBaseConstructor()
        {
            Console.WriteLine("Inside base class NULL parameterized constructor.");
        }
    }

    // Derived class declaration.
    class TestDerivedConstructor : TestBaseConstructor
    {
        // 2 overloaded constructors are defined as following.
        public TestDerivedConstructor(int x, int y)
        {
            Console.WriteLine("Inside derived class 2 parameterized constructor.");
        }

        public TestDerivedConstructor(int x)
            : base(x)
        {
            Console.WriteLine("Inside derived class 1 parameterized constructor.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 2 derived class objects are created.
            TestDerivedConstructor testDerivedConstructor1 = new TestDerivedConstructor(10, 10);
            TestDerivedConstructor testDerivedConstructor2 = new TestDerivedConstructor(50);
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Inside base class NULL parameterized constructor.
        Inside derived class 2 parameterized constructor.
        Inside base class 1 parameterized constructor.
        Inside derived class 1 parameterized constructor.
        
       */
    }
}


Example – 2, class members with base keyword:

using System;
namespace csharpBasic
{
    // A base class declaration.
    class MarkSheet
    {
        // Double type fields are declared.
        protected double CsharpMarks = 70;
        protected double JsMarks;
        protected double AspMarks;

        // Void type method is declared which displays obtain marks.
        public void GetObtainMarks()
        {
            Console.WriteLine("Obtain marks in base MarkSheet: {0}", this.CsharpMarks + this.AspMarks + this.JsMarks);
        }

        // Return type method is declared which returns percentage.       
        public double GetPercentage(double totalMarks)
        {
            double obtainMarks = this.CsharpMarks + this.AspMarks + this.JsMarks;
            return (obtainMarks / totalMarks) * 100.0;
        }
    }

    // A MarkSheet class inherits in BcsMarkSheet derived class.
    class BcsMarkSheet : MarkSheet
    {
        /* Following is the re-declaration of base class member field 
           with same name CsharpMarks in its derived class.*/
        new public double CsharpMarks;

        // Parameterized constructor which initializes each field.
        public BcsMarkSheet(double csharpMarks, double jsMarks, double aspMarks)
        {
            /* Notice still base class field CsharpMarks can be accessed/uncovered 
               through keyword “base” such as base. CsharpMarks. */
            base.CsharpMarks += csharpMarks;
            this.CsharpMarks = csharpMarks;
            this.JsMarks = jsMarks;
            this.AspMarks = aspMarks;
        }

        // Following methods return each subject’s marks.
        public double GetCsharpMarks()
        {
            return base.CsharpMarks;
        }
        public double GetJsMarks()
        {
            return this.JsMarks;
        }
        public double GetAspMarks()
        {
            return this.AspMarks;
        }

        /* Following is the re-declaration of base class member method with same 
           name GetObtainMarks() in its derived class. */
        new public void GetObtainMarks()
        {
            /* Notice still base class method GetObtainMarks() can be accessed/uncovered 
               through keyword “base” such as base. GetObtainMarks(). */
            base.GetObtainMarks();
            Console.WriteLine("Obtain marks in derived BcsMarkSheet: {0}", base.CsharpMarks + this.AspMarks + this.JsMarks);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Object creation of BcsMarkSheet
            BcsMarkSheet bcsMarkSheet = new BcsMarkSheet(20, 60, 87);

            // Following statements call different types of methods and display them.
            Console.WriteLine("Csharp base class marks: {0}", bcsMarkSheet.GetCsharpMarks());
            Console.WriteLine("Csharp derived class marks: {0}", bcsMarkSheet.CsharpMarks);
            Console.WriteLine("Javascript marks: {0}", bcsMarkSheet.GetJsMarks());
            Console.WriteLine("Asp.net marks: {0}", bcsMarkSheet.GetAspMarks());
            bcsMarkSheet.GetObtainMarks();
            Console.WriteLine("Percentage: {0} ", bcsMarkSheet.GetPercentage(300));
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        Csharp base class marks: 90
        Csharp derived class marks: 20
        Javascript marks: 60
        Asp.net marks: 87
        Obtain marks in base MarkSheet: 237
        Obtain marks in derived BcsMarkSheet: 237
        Percentage: 79
        
       */
    }
}





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 .