In C#, an interface is a contract that defines a set of methods, properties, events, or indexers, but does not provide any implementation. It is essentially a blueprint for classes or structs, specifying the functionality they should implement without dictating how that functionality should be carried out.
Characteristics of an Interface in C#
1. No Implementation: An interface only defines method signatures and properties, but does not provide the body (implementation) of those methods. The classes or structs that implement the interface must provide the actual implementation.
2. Multiple Implementations: A class or struct can implement multiple interfaces, allowing for flexible and modular design.
3. Public Access: All members of an interface are implicitly public. You cannot have private or protected members in an interface.
4. Inheritance: An interface can inherit from one or more other interfaces, which means it can combine multiple sets of functionality.
Syntax:
public interface IInterfaceName
{
// Method declarations (no implementation)
void Method1();
int Method2(string parameter);
// Property declarations (no implementation)
string Property { get; set; }
}
Note:
- interface keyword is used to define an interface.
- Methods, properties, or events are declared but not defined.
- An interface name usually starts with an “I” by convention
Example:
using System;
public interface IAnimal {
// // Method declaration (does not have a body)
void makeSound();
}
// Concrete class Dog implements Animal interface
public class Dog : IAnimal {
// Providing implementation for the abstract method makeSound()
public void makeSound() {
Console.WriteLine("Woof! Woof!");
}
}
// Concrete class Cat implements Animal interface
public class Cat : IAnimal {
// Providing implementation for the abstract method makeSound()
public void makeSound() {
Console.WriteLine("Meow! Meow!");
}
}
// Main class to test the interface and its implementation
public class MyProgram {
static void Main(String[] args) {
// Creating objects of Dog and Cat
Dog myDog = new Dog();
Cat myCat = new Cat();
// Calling the methods
myDog.makeSound(); // Dog's sound
myCat.makeSound(); // Cat's sound
}
}
Output:
Meow! Meow!
Explanation:
- Animal is an interface with an makeSound() method.
- Dog and Cat both implement the Animal interface and provide their own implementations of the makeSound() method.
Multiple Interfaces Implementation
C# allows a class to implement multiple interfaces, which is a way to achieve multiple inheritance.
Example:
using System;
// Interface definition
public interface Voice {
// method (does not have a body)
public void makeSound();
}
public interface Runnable {
// method (does not have a body)
public void run();
}
// Concrete class Dog implements Animal and Runnable interface
class Dog : Voice, Runnable {
// Providing implementation for the method makeSound() and run()
public void makeSound() {
Console.WriteLine("Woof! Woof!");
}
public void run() {
Console.WriteLine("Dog runs fast.");
}
}
// Main class to test the interface and its implementation
public class MyProgram {
static void Main(String[] args) {
// Creating objects of Dog
Dog myDog = new Dog();
// Calling the methods
myDog.makeSound(); // Dog's sound
myDog.run(); // Dog's run
}
}
Output:
Dog runs fast.
Why Use Interfaces?
1. To enforce a common set of methods/properties across different classes.
2. To support multiple inheritance of functionality (since a class can implement multiple interfaces).
3. To achieve loose coupling in your design, making it easier to modify, extend, or replace parts of your application.