In C#, the basekeyword is used to access members of the base class from within a derived class. It can be used to:
Call a method on the base class that has been overridden by another method.
Specify which base-class constructor should be called when creating instances of the derived class.
For example, consider the following code:
C#
class BaseClass
{
public void BaseMethod()
{
Console.WriteLine("This is the BaseMethod() method.");
}
}
class DerivedClass : BaseClass
{
public override void BaseMethod()
{
Console.WriteLine("This is the DerivedClass.BaseMethod() method.");
}
}
class MainClass
{
public static void Main()
{
DerivedClass derivedClass = new DerivedClass();
derivedClass.BaseMethod();
}
}
In this code, the BaseMethod() method is overridden in the
DerivedClass class. When the BaseMethod() method is called on the
derivedClass object, the overridden version of the method is executed. However, if we want to call the original version of the
BaseMethod() method, we can use the base keyword. For example:
C#
derivedClass.BaseMethod(); // Calls the overridden version of the method
base.BaseMethod(); // Calls the original version of the method
The base keyword can also be used to specify which base-class constructor should be called when creating instances of the derived class. For example:
C#
class BaseClass
{
public BaseClass()
{
Console.WriteLine("This is the BaseClass() constructor.");
}
}
class DerivedClass : BaseClass
{
public DerivedClass()
{
Console.WriteLine("This is the DerivedClass() constructor.");
}
}
class MainClass
{
public static void Main()
{
DerivedClass derivedClass = new DerivedClass(); // Calls the DerivedClass() constructor
derivedClass = new DerivedClass(10); // Calls the BaseClass() constructor
}
}
In this code, the DerivedClass class has two constructors. The default constructor calls the
BaseClass constructor, and the parameterized constructor does not call the
BaseClass constructor. When we create a DerivedClass object without passing any parameters, the default constructor is called. However, if we pass a parameter to the
DerivedClass constructor, the parameterized constructor is called, and the
BaseClass constructor is not called.
The base keyword is a powerful tool that can be used to access members of the base class from within a derived class. It can be used to call methods, specify constructors, and more.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C#, the
basekeyword is used to access members of the base class from within a derived class. It can be used to:For example, consider the following code:
C#
In this code, the
BaseMethod()method is overridden in theDerivedClassclass. When theBaseMethod()method is called on thederivedClassobject, the overridden version of the method is executed. However, if we want to call the original version of theBaseMethod()method, we can use thebasekeyword. For example:C#
The
basekeyword can also be used to specify which base-class constructor should be called when creating instances of the derived class. For example:C#
In this code, the
DerivedClassclass has two constructors. The default constructor calls theBaseClassconstructor, and the parameterized constructor does not call theBaseClassconstructor. When we create aDerivedClassobject without passing any parameters, the default constructor is called. However, if we pass a parameter to theDerivedClassconstructor, the parameterized constructor is called, and theBaseClassconstructor is not called.The
basekeyword is a powerful tool that can be used to access members of the base class from within a derived class. It can be used to call methods, specify constructors, and more.