base keyword in c#

 
# base keyword:- It basically used to access constructors and methods of the base class. The base keyword cannot use within a static method.

Use of Base keyword:-

·Call the method of base class that has been overridden by another method.or Base keyword call the hidden method of base class in the derived class.
 
Example:-

  public class BaseClass
    {
        public virtual void basePrint()
        {
            Console.WriteLine("I am in base class");
        }
    }  
   
    class DeriveClass:BaseClass
    {
        public override void basePrint()
        {
            Console.WriteLine("I am in derived class");  
        }
       
        static void Main()
        {           
            DeriveClass obj = new DeriveClass();
            obj.basePrint();
 
            //BaseClass obj1 = new BaseClass();
            //obj1.basePrint();
        }
    }   
 
    //Here base class method basePrint() has been overriden by derived class  method basePrint();
 
Solution:-
   public override void basePrint()
   {
      base.basePrint();
      Console.WriteLine("I am in derived class");  
    }
 


·Base keyword specifies which constructor of the base class should be invoked while creating the instances of the derived class.

Example:-
           
public partial class EmployeeContext : DbContext
{
    public EmployeeContext(): base("name=AppConnection")
    {
    }
}
 

 


No comments:

Post a Comment