Introduction
The base keyword helps you use features from a parent class inside a child class. It lets you reuse and extend code easily.
Jump into concepts and practice - no test required
The base keyword helps you use features from a parent class inside a child class. It lets you reuse and extend code easily.
class ChildClass : ParentClass { public ChildClass() : base() { } public override void SomeMethod() { base.SomeMethod(); // additional code } }
base() calls the parent class constructor.
base.MethodName() calls a method from the parent class.
base.Show() calls the Show method from the parent class inside the child class.class Parent { public void Show() => Console.WriteLine("Parent Show"); } class Child : Parent { public void ShowChild() { base.Show(); Console.WriteLine("Child Show"); } }
base() before running its own code.class Parent { public Parent() { Console.WriteLine("Parent constructor"); } } class Child : Parent { public Child() : base() { Console.WriteLine("Child constructor"); } }
Greet but still calls the parent's version first using base.Greet().class Parent { public virtual void Greet() => Console.WriteLine("Hello from Parent"); } class Child : Parent { public override void Greet() { base.Greet(); Console.WriteLine("Hello from Child"); } }
This program shows how the base keyword calls the parent constructor and method from the child class.
using System; class Parent { public Parent() { Console.WriteLine("Parent constructor called"); } public virtual void Display() { Console.WriteLine("Display method in Parent"); } } class Child : Parent { public Child() : base() { Console.WriteLine("Child constructor called"); } public override void Display() { base.Display(); Console.WriteLine("Display method in Child"); } } class Program { static void Main() { Child c = new Child(); c.Display(); } }
You cannot use base in static methods because base refers to instance members.
If the parent class method is not marked virtual or abstract, you cannot override it but can still call it with base.
The base keyword lets child classes access parent class members.
Use base() to call the parent constructor.
Use base.Method() to call a parent method inside an overridden method.
base keyword do in C#?basebase keyword is used in a child class to refer to its parent class members.base allowsbase keyword = access parent members [OK]base in C#?: base() after the child constructor signature.public Child() : base() { }, which is the correct syntax.: base() [OK]class Parent {
public virtual void Show() {
Console.WriteLine("Parent Show");
}
}
class Child : Parent {
public override void Show() {
base.Show();
Console.WriteLine("Child Show");
}
}
var obj = new Child();
obj.Show();Child class overrides Show() and calls base.Show() which runs the Parent version first.base.Show(), then "Child Show" is printed from the child method.class Parent {
public void Display() {
Console.WriteLine("Parent Display");
}
}
class Child : Parent {
public override void Display() {
base.Display();
Console.WriteLine("Child Display");
}
}virtual or abstract in the parent can be overridden.Display() method in Parent is not virtual, so override in Child causes a compile error.class A {
public virtual string GetName() => "A";
}
class B : A {
public override string GetName() => "B";
}
class C : B {
public override string GetName() => base.GetName() + "C";
}
var obj = new C();
Console.WriteLine(obj.GetName());C overrides GetName() and calls base.GetName(), which refers to B's override returning "B".C appends "C" to the result from B, so the final string is "B" + "C" = "BC".