---
title: "Interface in C#.Net"  
description: "An interface contains only the signatures of methodsdelegates or events An interface looks like a class, but has no implementation. The only thing it"  
author: "AVADHESH PATEL"  
published: 2012-07-10  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/321/interface-in-c-sharp-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Interface in C#.Net

An [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) contains only the signatures of methods, delegates or events. An interface looks like a class, but has no [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp). The only thing it contains is declarations of events, [indexers](https://www.mindstick.com/interview/406/what-are-the-indexers), methods and/or properties. Interfaces in C # provide a way to [achieve runtime](https://www.mindstick.com/interview/2390/can-you-achieve-runtime-polymorphism-by-data-members-in-java) polymorphism.

##### Why Use Interfaces?

To allows a class to inherit multiple behaviors from [multiple interfaces](https://www.mindstick.com/interview/2599/can-you-inherited-multiple-interfaces).

To avoid name ambiguity between the methods of the different classes as was in the use of multiple inheritances in C++.

To [combine two](https://www.mindstick.com/forum/160711/how-to-combine-two-arrays-without-duplicate-values-in-c-sharp) or more interfaces such that a class need only implement the combined result. The example code for this is provided as a source file to be downloaded.

To allows [Name hiding](https://www.mindstick.com/forum/159543/describe-the-name-hiding-issue-in-c-plus-plus-and-how-it-might-lead-to-logical-errors): Name hiding is the ability to hide an inherited member name from any code outside the [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class).

##### Defining an Interface

```
interface IMyInterface{
void MethodToImplement();}{
void MethodToImplement();}
```

##### Implementation of Interface

```
class InterfaceImplementer : IMyInterface{
static void Main(){InterfaceImplementer iImp =new InterfaceImplementer();iImp.MethodToImplement();}
public void MethodToImplement(){Console.WriteLine("MethodToImplement() called.");}}{
static void Main(){InterfaceImplementer iImp =new InterfaceImplementer();iImp.MethodToImplement();}
public void MethodToImplement(){Console.WriteLine("MethodToImplement() called.");}}{static void Main(){InterfaceImplementer iImp =new InterfaceImplementer();iImp.MethodToImplement();}public void MethodToImplement(){Console.WriteLine("MethodToImplement() called.");}}{static void Main(){InterfaceImplementer iImp =new InterfaceImplementer();iImp.MethodToImplement();}public void MethodToImplement(){Console.WriteLine("MethodToImplement() called.");}}{ static void Main(){InterfaceImplementer iImp =new InterfaceImplementer();iImp.MethodToImplement();}public void MethodToImplement(){Console.WriteLine("MethodToImplement() called.");}}
```

##### Compile example:

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace @interface{    interface IMyInterface    {        void MethodToImplement();    }        class InterfaceImplementer : IMyInterface    {        static void Main()        {            InterfaceImplementer iImp = new InterfaceImplementer();            iImp.MethodToImplement();            Console.ReadLine();        }        public void MethodToImplement()        {            Console.WriteLine("MethodToImplement() called.");        }    }}
```

##### Output:

## MethodToImplement() called.

##

##

##

##

---

Original Source: https://www.mindstick.com/blog/321/interface-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
