Codementor Events

C# Interfaces

Published Jan 03, 2022

Start writing here...Interface are powerful programing tool that will help you to separate the definition of the object from their implementation.

An interface is a contract between itself. What is meant by this contract, any class that implement Interface must implement the Properties, Methods and Events?

You define an interface by using the interface keyword, as the following example shows

interface IEquatable<T>

{

bool Equals(T obj);

}

Any class or struct that implements the IEquatable<T> interface must contain a definition for an Equals method that matches the signature that the interface specifies.

Disadvantage

Interface always force you implement all the methods which are not required in your case. That means you have to write some dummy method to implement all these methods.

Advantage:

Interface members are automatically public by default.

Members also can’t be static

You can implement multiple inheritance with the help of interface.

Question:

Why we can’t use multiple inheritance in C# classes.

Because of “Deadly diamond of death”

interface

The diamond Problem is an ambiguity that arises when 2 classes B and C inherits from A,

And class D inherits from both B and C.

If there is a method in A that B and C has overridden and D doesn’t overridden it, then which version of the method does D inherit: That of B or That of C?

Specification:

A class that implement interface must implement all the member.

Interface can’t be instantiated directly.

Interfaces can contain events, indexers, methods, and properties.

A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.

We can implement interface in two ways Implicit and Explicit.

When we define and declare interface implicit that means we are defining as public. Means it opens for everyone.

Example of implicit Interface.

Interface IDisplayDetails

{

void display()

}

class Class1 : IDisplayDetails

{

public void Display()

{

Console.WriteLine(“Class1 Display Method.”);

}

}

class Program

{

static void Main(string[] args)

{

Class1 objClass1 = new Class1();

objClass1.Display();

}

}

Output: Class1 Display Method

How to implement Explicit Interface.

Suppose Class is implementing two interfaces and these two interface have method with same signature, then implementing that member on that class will cause both interface to use that member as their implementation.

interface IControl

{

void Paint();

}

interface ISurface

{

void Paint();

}

public class SampleClass : IControl, ISurface

{

void IControl.Paint()

{

System.Console.WriteLine(“IControl.Paint”);

}

void ISurface.Paint()

{

System.Console.WriteLine(“ISurface.Paint”);

}

}

SampleClass obj = new SampleClass();

//obj.Paint(); // Compiler error.

IControl c = (IControl)obj;

c.Paint(); // Calls IControl.Paint on SampleClass.

ISurface s = (ISurface)obj;

s.Paint()

Discover and read more posts from Venkatesh Mahendru
get started
post commentsBe the first to share your opinion
Show more replies