In C#, polymorphism is the ability to use a variable of one type to refer to an object of another type. This is possible because C# supports inheritance, which allows classes to inherit from other classes.
Polymorphism is implemented in C# using the is and as keywords. The
is keyword can be used to check if a variable is of a particular type. The
as keyword can be used to cast a variable to a different type.
For example, the following code defines a class Animal and a derived class
Dog:
C#
class Animal
{
}
class Dog : Animal
{
}
The following code creates a variable of type Animal and assigns it to an object of type
Dog:
C#
Animal animal = new Dog();
The animal variable is now of type Animal, but it is also pointing to an object of type
Dog. This is because Dog inherits from Animal.
The following code uses the is keyword to check if the animal variable is of type
Dog:
C#
bool isDog = animal is Dog;
The isDog variable will be true because the animal variable is pointing to an object of type
Dog.
The following code uses the as keyword to cast the animal variable to a type
Dog:
C#
Dog dog = animal as Dog;
If the animal variable is actually of type Dog, the
dog variable will be assigned the value of the animal variable. Otherwise, the
dog variable will be null.
Polymorphism is a powerful feature that can be used to make code more flexible and reusable. It can be used to create generic code that can work with objects of different types.
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#, polymorphism is the ability to use a variable of one type to refer to an object of another type. This is possible because C# supports inheritance, which allows classes to inherit from other classes.
Polymorphism is implemented in C# using the
isandaskeywords. Theiskeyword can be used to check if a variable is of a particular type. Theaskeyword can be used to cast a variable to a different type.For example, the following code defines a class
Animaland a derived classDog:C#
The following code creates a variable of type
Animaland assigns it to an object of typeDog:C#
The
animalvariable is now of typeAnimal, but it is also pointing to an object of typeDog. This is becauseDoginherits fromAnimal.The following code uses the
iskeyword to check if theanimalvariable is of typeDog:C#
The
isDogvariable will betruebecause theanimalvariable is pointing to an object of typeDog.The following code uses the
askeyword to cast theanimalvariable to a typeDog:C#
If the
animalvariable is actually of typeDog, thedogvariable will be assigned the value of theanimalvariable. Otherwise, thedogvariable will benull.Polymorphism is a powerful feature that can be used to make code more flexible and reusable. It can be used to create generic code that can work with objects of different types.