Interfaces in C#
An interface is a contract between a class and the outside world. It defines a set of methods and properties that a class must implement. C# supports multiple inheritance of interfaces, which means that a class can inherit from multiple interfaces. Here's an example code that demonstrates how to define an interface and implement it in a class:
interface IAnimal
{
void MakeSound();
}
class Dog : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Woof");
}
}
class Cat : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Meow");
}
}
Dog d = new Dog();
d.MakeSound(); // prints "Woof"
Cat c = new Cat();
c.MakeSound(); // prints "Meow"
Exception Handling:
No comments:
Post a Comment