In C#, a virtual method is defined in a base class and can be overridden in a derived class. This allows for dynamic method binding, where the derived class’s method is called even when using a base class reference.
class Employee {public virtual void MakeHRRequest() {Console.WriteLine("Employee makes an HR request.");}}class Manager : Employee {public override void MakeHRRequest() {Console.WriteLine("Manager makes an HR request.");}}
The is
operator in C# checks if a reference can be cast to a specified type. It returns True
if the conversion is possible and False
otherwise.
public class Animal{public void makeSound(){Console.WriteLine("Animal makes a sound");}}public class Dog : Animal{public void makeSound(){Console.WriteLine("Dog barks");}}public class Program{public static void Main(string[] args){Dog myDog = new Dog();Console.WriteLine(myDog is Animal); // Outputs True}}
The as
operator in C# attempts to downcast a reference to a specified type. If the downcast fails, it returns null
instead of throwing an exception.
public class Animal{public virtual void makeSound(){Console.WriteLine("Animal makes a sound");}}public class Dog : Animal{public override void makeSound(){Console.WriteLine("Dog barks");}public void move(){Console.WriteLine("Dog walks");}}public class Program{public static void Main(string[] args){Animal myAnimal = new Dog();// myAnimal.move(); // Will not workDog myDog = myAnimal as Dog;if (myDog != null){myDog.makeSound(); // Output: Dog barksmyDog.move(); // Output: Dog walks}}}
Downcasting in C# is the explicit conversion from a base class reference to a derived class reference. It requires a cast operator and can throw an exception if the object isn’t of the derived type.
Employee myEmployee = new Manager(); // Upcasting to EmployeeManager myManager = (Manager)myEmployee; // Downcasting to Manager
Upcasting in C# is the implicit conversion from a derived class to a base class. This allows a derived class object to be treated as an instance of its base class.
Manager myManager = new Manager();Employee myEmployee = myManager; // Upcasting to Employee
An abstract class in C# cannot be instantiated and is meant to be a base class for other classes. It can contain both abstract methods (without implementation) and concrete methods (with implementation).
abstract class Employee {public abstract void MakeHRRequest(); // Must be implemented by the derived classpublic void ClockIn() {Console.WriteLine("Employee clocks in.");}}
An abstract method in C# is declared within an abstract class and does not have an implementation. It must be overridden in any derived class that inherits from the abstract class.
abstract class Employee {public abstract void MakeHRRequest();}class Manager : Employee {public override void MakeHRRequest() {Console.WriteLine("Manager makes an HR request.");}}
In C#, references are polymorphic, meaning a variable of a base type can refer to an object of a derived class. This allows for flexibility in code, enabling methods to operate on base class references, regardless of the specific subclass type.