The base Keyword
In this page:
The base Keyword
base refers to the immediate parent class from inside a derived class, letting you call the base constructor or an overridden base method explicitly. base(args) in a constructor forwards initialization work to the parent. This avoids duplicating logic that the base class already handles.
Note: Use base.MethodName() inside an override when you want to extend, not replace, the parent's behavior.
Example: The base Keyword
using System;
class Animal
{
public string Name;
public Animal(string name)
{
Name = name;
}
public virtual void Describe()
{
Console.WriteLine($"I am {Name}");
}
}
class Dog : Animal
{
public Dog(string name) : base(name) { }
public override void Describe()
{
base.Describe();
Console.WriteLine("...and I am a dog");
}
}
class Program
{
static void Main(string[] args)
{
Dog d = new Dog("Rex");
d.Describe();
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: