C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Interface

In this tutorial, we will explore interfaces in C#, an important concept in object-oriented programming. Interfaces define a contract that classes or structs can implement, specifying a set of properties, methods, and events that the implementing class or struct must provide.

  • Defining an Interface

To define an interface, use the interface keyword followed by the interface name. Interface names typically start with an "I" to differentiate them from classes:

public interface ISampleInterface
{
    // Interface members
}
  • Interface Members

Interfaces can contain properties, methods, and events. All interface members are implicitly public, and you cannot specify an access modifier. Interface members also do not have an implementation in the interface; the implementation must be provided by the implementing class or struct:

public interface IAnimal
{
    string Name { get; set; }
    int Age { get; set; }

    void Speak();
    void Eat();
}

In this example, the IAnimal interface contains two properties (Name and Age) and two methods (Speak and Eat).

  • Implementing an Interface

To implement an interface in a class or struct, use the : operator followed by the interface name. The class or struct must provide an implementation for all members of the interface:

public class Dog : IAnimal
{
    public string Name { get; set; }
    public int Age { get; set; }

    public void Speak()
    {
        Console.WriteLine($"{Name} barks.");
    }

    public void Eat()
    {
        Console.WriteLine($"{Name} eats.");
    }
}

In this example, the Dog class implements the IAnimal interface by providing implementations for the Name, Age, Speak, and Eat members.

  • Using Interfaces

Interfaces allow you to write more generic and reusable code. You can treat objects of different types that implement the same interface as objects of a common type:

IAnimal dog = new Dog { Name = "Buddy", Age = 5 };
dog.Speak(); // Output: "Buddy barks."
dog.Eat();   // Output: "Buddy eats."

In this example, the dog variable is of type IAnimal, even though it holds an instance of the Dog class. This allows you to use the IAnimal interface members on the dog object.

  • Implementing Multiple Interfaces

A class or struct can implement multiple interfaces by separating them with a comma:

public interface IMovable
{
    void Move();
}

public class Dog : IAnimal, IMovable
{
    // Implement IAnimal members...

    public void Move()
    {
        Console.WriteLine($"{Name} moves.");
    }
}

In this example, the Dog class implements both the IAnimal and IMovable interfaces.

This tutorial demonstrates the basics of interfaces in C#. Interfaces define a contract that classes or structs can implement, specifying a set of properties, methods, and events. By implementing interfaces, you can create more generic and reusable code, allowing objects of different types that implement the same interface to be treated as objects of a common type.

  1. How to use interfaces in C#

    Interfaces define a contract that classes can implement, ensuring that they provide specific methods or properties. Here's a basic example:

    using System;
    
    // Interface declaration
    interface IExample
    {
        void Display();
    }
    
    // Class implementing the interface
    class MyClass : IExample
    {
        public void Display()
        {
            Console.WriteLine("Implementation of Display method");
        }
    }
    
    class Program
    {
        static void Main()
        {
            // Creating an object of the implementing class
            MyClass myObject = new MyClass();
    
            // Accessing the method through the interface
            myObject.Display();
    
            Console.ReadLine();
        }
    }
    
  2. C# interface example

    The example above demonstrates a simple interface (IExample) with a method (Display) and a class (MyClass) implementing that interface.

  3. Implementing interfaces in C#

    To implement an interface, use the : syntax after the class declaration, followed by the interface name. Example:

    interface IExample
    {
        void Display();
    }
    
    class MyClass : IExample
    {
        public void Display()
        {
            // Implementation of the Display method
        }
    }
    
  4. C# explicit interface implementation

    Explicit interface implementation is used when a class implements an interface and wants to avoid naming conflicts. Example:

    interface IExample
    {
        void Display();
    }
    
    class MyClass : IExample
    {
        void IExample.Display()
        {
            Console.WriteLine("Explicit implementation of Display method");
        }
    }
    
  5. Multiple interface implementation in C#

    A class can implement multiple interfaces by separating them with commas. Example:

    interface IFirstInterface
    {
        void MethodA();
    }
    
    interface ISecondInterface
    {
        void MethodB();
    }
    
    class MyClass : IFirstInterface, ISecondInterface
    {
        public void MethodA() { /* Implementation */ }
        public void MethodB() { /* Implementation */ }
    }
    
  6. C# interface properties and methods

    Interfaces can contain both properties and methods. Example:

    interface IExample
    {
        void Display();
        int Value { get; set; }
    }
    
    class MyClass : IExample
    {
        public void Display() { /* Implementation */ }
        public int Value { get; set; }
    }
    
  7. Using interfaces for polymorphism in C#

    Interfaces enable polymorphism, allowing objects of different classes to be treated as instances of the common interface. Example:

    interface IShape
    {
        void Draw();
    }
    
    class Circle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Drawing a circle");
        }
    }
    
  8. Inheriting interfaces in C#

    Interfaces can inherit from other interfaces. Example:

    interface IBase
    {
        void BaseMethod();
    }
    
    interface IDerived : IBase
    {
        void DerivedMethod();
    }
    
  9. C# interface naming conventions

    Interface names in C# typically start with an I. For example, IExample.

  10. Interface inheritance in C#

    Interfaces can inherit from other interfaces, creating a hierarchy of contracts. Example:

    interface IBase
    {
        void BaseMethod();
    }
    
    interface IDerived : IBase
    {
        void DerivedMethod();
    }