Introduction to Interfaces
Interfaces in C# are a fundamental part of object-oriented programming. They define a contract that classes or structs can implement. An interface specifies what methods, properties, events, or indexers a class or struct must implement, but it does not provide the implementation itself.
Key Concepts
- Definition: An interface is defined using the
interface
keyword. - Implementation: A class or struct that implements an interface must provide the implementation for all its members.
- Multiple Inheritance: A class can implement multiple interfaces, providing a way to achieve multiple inheritance in C#.
- Polymorphism: Interfaces allow for polymorphic behavior, enabling objects to be treated as instances of their interface types.
Syntax
Example
Let's create an interface IShape
and implement it in two classes, Circle
and Rectangle
.
Interface Definition
Implementing the Interface
public class Circle : IShape { public double Radius { get; set; } public Circle(double radius) { Radius = radius; } public double Area() { return Math.PI * Radius * Radius; } public double Perimeter() { return 2 * Math.PI * Radius; } } public class Rectangle : IShape { public double Width { get; set; } public double Height { get; set; } public Rectangle(double width, double height) { Width = width; Height = height; } public double Area() { return Width * Height; } public double Perimeter() { return 2 * (Width + Height); } }
Using the Interface
public class Program { public static void Main() { IShape circle = new Circle(5); IShape rectangle = new Rectangle(4, 6); Console.WriteLine($"Circle Area: {circle.Area()}"); Console.WriteLine($"Circle Perimeter: {circle.Perimeter()}"); Console.WriteLine($"Rectangle Area: {rectangle.Area()}"); Console.WriteLine($"Rectangle Perimeter: {rectangle.Perimeter()}"); } }
Explanation
- Interface Definition: The
IShape
interface defines two methods:Area
andPerimeter
. - Circle Class: Implements the
IShape
interface and provides the implementation forArea
andPerimeter
methods. - Rectangle Class: Also implements the
IShape
interface and provides its own implementation forArea
andPerimeter
methods. - Program Class: Demonstrates how to use the
IShape
interface to create instances ofCircle
andRectangle
and call their methods.
Practical Exercises
Exercise 1: Define and Implement an Interface
Task: Define an interface IVehicle
with methods StartEngine
and StopEngine
. Implement this interface in two classes: Car
and Bike
.
Solution
public interface IVehicle { void StartEngine(); void StopEngine(); } public class Car : IVehicle { public void StartEngine() { Console.WriteLine("Car engine started."); } public void StopEngine() { Console.WriteLine("Car engine stopped."); } } public class Bike : IVehicle { public void StartEngine() { Console.WriteLine("Bike engine started."); } public void StopEngine() { Console.WriteLine("Bike engine stopped."); } } public class Program { public static void Main() { IVehicle car = new Car(); IVehicle bike = new Bike(); car.StartEngine(); car.StopEngine(); bike.StartEngine(); bike.StopEngine(); } }
Exercise 2: Interface with Properties
Task: Define an interface IEmployee
with properties Name
and Salary
. Implement this interface in a class Employee
.
Solution
public interface IEmployee { string Name { get; set; } double Salary { get; set; } } public class Employee : IEmployee { public string Name { get; set; } public double Salary { get; set; } public Employee(string name, double salary) { Name = name; Salary = salary; } } public class Program { public static void Main() { IEmployee employee = new Employee("John Doe", 50000); Console.WriteLine($"Employee Name: {employee.Name}"); Console.WriteLine($"Employee Salary: {employee.Salary}"); } }
Common Mistakes and Tips
- Not Implementing All Members: Ensure that all members of the interface are implemented in the class.
- Interface Naming Convention: By convention, interface names start with an uppercase 'I'.
- Multiple Interfaces: A class can implement multiple interfaces, separated by commas.
Summary
In this section, we covered the basics of interfaces in C#. We learned how to define an interface, implement it in classes, and use it to achieve polymorphism. We also provided practical exercises to reinforce the concepts. Understanding interfaces is crucial for designing flexible and maintainable code in C#.
C# Programming Course
Module 1: Introduction to C#
- Introduction to C#
- Setting Up the Development Environment
- Hello World Program
- Basic Syntax and Structure
- Variables and Data Types
Module 2: Control Structures
Module 3: Object-Oriented Programming
- Classes and Objects
- Methods
- Constructors and Destructors
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Module 4: Advanced C# Concepts
- Interfaces
- Delegates and Events
- Generics
- Collections
- LINQ (Language Integrated Query)
- Asynchronous Programming
Module 5: Working with Data
Module 6: Advanced Topics
- Reflection
- Attributes
- Dynamic Programming
- Memory Management and Garbage Collection
- Multithreading and Parallel Programming