Friday, 23 December 2016

base Keyword in C# example



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Base_Keyword_in_Csharp
{
    class Person {
        protected string  FirstName { get; set; }
        protected string LastName { get; set; }

        public Person() //without public we can't inherit Person.
        {
            this.FirstName = "Default First Name";
            this.LastName = "Default Last Name";
        }

        public Person(string firstname, string lastname) //if we make private Person(string,string) inaccessible due to protraction level
        {
            this.FirstName = firstname;
            this.LastName = lastname;
        }

        public virtual void GetInfo() { //virtual method could not be private
            Console.WriteLine("First Name:" + FirstName);
            Console.WriteLine("Last Name:" + LastName);
        }

    }

    class Employee : Person //if we don't create least a constructor  as public we would not able to inherit them.
    {

        public string EmpID = "EMP001";

        Employee() : base()
        {

        }
        Employee(string fname, string lname):base(fname, lname)
        {

        }

        public override void GetInfo()
        {
            base.GetInfo(); // to call base class virtual function.
            Console.WriteLine("Employee ID:" + EmpID);
        }


        static void Main(string[] args)
        {

            Employee emp = new Employee();//works if only 1 constructor in derive class,  if 2 we can't use until we define base constructor in derived class.
            emp.GetInfo();

            Console.WriteLine("___________________________________________");

            Employee emp2 = new Employee("raj", "kumar");//works if only 1 constructor in derive class,  if 2 we can't use until we define base constructor in derived class.
            emp2.GetInfo();



            Console.Read();
        }
    }
}

/*
Output:

First Name:Default First Name
Last Name:Default Last Name
Employee ID:EMP001
___________________________________________
First Name:raj
Last Name:kumar
Employee ID:EMP001

 Interesting facts
 We can not inherit private class.
 In a concrete class we can create a virtual function that may be later override in derive or not.
 We must have a public constructor in base class so child would derived from them.
 If we don't declare constructor in base .net automatically defined a public constructor.
 After overriding virtual function we may call base method or not. we can call like:- base.GetInfo();
 We can access all constructor of base by defining in child using base keyword.


Overall:
The base keyword is used to access members of the base class from within a derived class:
1.Call a method on the base class that has been overridden by another method.
2.Specify which base-class constructor should be called when creating instances of the derived class.

Reference: https://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx

     */