Interface Interview Question in C#

Below are the interface interview questions in C# for beginners and experienced developers

What is the use of interface?

Interface mostly used for following reasons:

  • Specifying contract
  • Multiple inheritances
  • Loosely coupled code

Specifying contract – Interface is used to define a contract in the application, so that the other application should follow the contract in order to execution. In other words, we can say that we also specifying what class can do. So, inheriting an interface also means that class must implement all the methods.

public interface IDatabase
{
    void Insert();
    void Update();
    void Delete();
}

//Class should implement all members of interface otherwise will get compile-time error
public class SQLDatabase : IDatabase
{
    public void Insert()
    {
        throw new NotImplementedException();
    }

    public void Update()
    {
        throw new NotImplementedException();
    }

    public void Delete()
    {
        throw new NotImplementedException();
    }
}

Multiple inheritances – Interface solved the real-time issue of Multiple inheritances, using interface a class can inherit with multiple interfaces.

public class ClassName : IInterface1, IInterface2, IInterface3
{
	//Implementing all the interface methods
}

Loosely coupled code – when we a class create an object of another class then its tightly coupled code. Interface allows us to write loosely coupled code by following open-close principals (open for extension close for modification).

public interface IDatabase
{
    void Insert();
    void Update();
    void Delete();
}

//Class should implement all members of interface otherwise will get compile-time error
public class SQLDatabase : IDatabase
{
    public void Insert()
    {
        throw new NotImplementedException();
    }

    public void Update()
    {
        throw new NotImplementedException();
    }

    public void Delete()
    {
        throw new NotImplementedException();
    }
}

Business logic – creating instance of SQLDatabase


public class BusinessLogic
{
    private IDatabase database;
    public BusinessLogic()
    {
        database = GetDatabaseInstance();
    }

    public void Insert()
    {
        database.Insert();
    }

    public IDatabase GetDatabaseInstance()
    {
        //Use DI or Factory pattern to return required Data Access Layer
    }
}

Why Interface methods cannot have access modifiers?

The purpose of the interface is to create contracts/rules for a class so that other class could you access the methods from it. So, there is no point by having access modifiers in an interface as it is created to let other classes implement all members. Adding access modifiers inside interface will break the purpose of an interface also it will restrict while implementing all the members of a class. Hence Interface methods do not have access modifiers.


Can we add properties in Interface?

Yes, we can add properties without the body in Interface.

We can add properties like shown in below code snippet

public interface ITest
{
    string Type { get; set; }
}

We can not add properties shown in below code snippet

public interface ITest
{
    int myVar;

    int MyProperty
    {
        get { return myVar; }
        set { myVar = value; }
    }

}

What are the advantages of an Interface?

  • The interface allows us to be more abstract.
  • Interfaces allow us to create/develop loosely coupled applications
  • Interfaces enable to achieve multiple inheritances in C#
  • The interface enables to write a structure code and enable parallel development
  • Interfaces allow mocking for better unit testing.
  • Interfaces are great for implementing Inversion of Control.

what is Explicitly Implementing an Interface?

When a class explicitly use interface name while implementing an interface is called as Explicitly Implementing. Some time while implementing a class we have two methods with the same name in interfaces that time we must use interface name explicitly.

public interface ITest
{
    void Test();

}

public interface ITest1 : ITest
{
    void Test();

}

public class TempClass : ITest1
{
    void ITest.Test()
    {
        throw new NotImplementedException();
    }

    void ITest1.Test()
    {
        throw new NotImplementedException();
    }
}

What is implicit implementation?

When there is no need to explicitly use interface while implementing interface methods is called implicit implementation.

public interface IDatabase
{
    void Insert();
    void Update();
    void Delete();
}

//Class should implement all members of interface otherwise will get compile-time error
public class SQLDatabase : IDatabase
{
    public void Insert()
    {
        throw new NotImplementedException();
    }

    public void Update()
    {
        throw new NotImplementedException();
    }

    public void Delete()
    {
        throw new NotImplementedException();
    }
}

Why C# does not support multiple class inheritance?

Multiple inheritances are deriving a class from multiple parents. Multiple inheritances lead to Diamond problem. The Diamond problem is an ambiguity that arises when two classes B and C inherit from Class A and Class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B or C?
Because of this ambiguity reason designer of C# languages does not support multiple inheritances. As it would be adding more complexity with fewer benefits.


Can you create instance of an interface?

No, we cannot create an instance of an interface.


When to use interface and when to use an abstract class?

Abstract classes are used to defining characteristics of an object type; agreeing what an object is about. Whereas interface is specifying what the object can do.
So abstract for what a class is, interface for what a class can do. Below are some of the rules but those are not always we have to follow

  • If the functionality is useful across a wide range of the disparate object, then use interface. As abstract class are helpful in an object that closely related. The interface is best suited for common functionality to an unrelated class.
  • If we anticipate creating multiple versions of the component, create an abstract class. Abstract classes provide a simple and easy way to version the components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created in that way. If a new version of an interface is required, we must create a whole new interface.

Differences between interfaces and abstract classes?

c# advanced interview questions, c# interview questions, interface vs abstract class c#, when to use abstract class in c#

Can we add fields in interface?

No, we cannot add fields in interface.


What is the difference between class inheritance and interface inheritance?

When we inherit from the interface, we have to implement all the interface members in a class. The interface allows inheriting from multiple interfaces. class inheritance allows inheriting only from one class.


Can we mark the interface method as virtual in the derived class?

Yes, we can mark an interface as virtual in derived class so the method can be overridden


Read more about the interface and also take free online multiple choice question (MCQ) Interface quiz to test your knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *