C# interface and multiple interface tutorial
Tags: csharp, dotnet, tutorial, programming
Published: 2021-02-02 11:25:25

<!-- wp:heading -->
<h2>What is interface?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The definitions for a group of related functionalities that a non-abstract class or struct can declare is called an interface. The interface is another way of abstraction in C#. The interface is like the abstract class but it only contains the methods definitions, whereas the abstract class contains all methods, fields, constants definitions. Unlike the abstract class, the interface cannot have a constructor. The interface methods should be abstract and public. The interface only provides the definition for the methods. In C# multiple inheritances cannot be done in class but by means of the interface.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>The interface in C#:</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Members in the interface should be abstract and public only no other access specifier used.</li><li>By interface, we can’t able to create an object like an abstract class.</li><li>Interface only define the methods, the class needed to override and implement those methods.</li><li>The interface doesn’t contain a constructor.</li><li>The interface contains methods and properties but not fields.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Implementation of C# interface:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The interface keyword used to implement the interface. We can also implement multiple interfaces for a single class in C#. It's suggested to use I before every interface name to identify it has the interface. To use an interface in a class we use (:) to access the interface. For example, demo as interface name then use <code>IDemo</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Syntax:</h2>
<!-- /wp:heading -->

<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: #f0f0f0; color: #444444;"><span class="hljs-keyword" style="font-weight: bold;">interface</span> &lt;<span class="hljs-title" style="color: #880000; font-weight: bold;">interface_name</span>&gt;
{
    method definition;
}
<span class="hljs-keyword" style="font-weight: bold;">class</span> &lt;<span class="hljs-title" style="color: #880000; font-weight: bold;">class_name</span>&gt;:&lt;<span class="hljs-title" style="color: #880000; font-weight: bold;">interface_name</span>&gt;
{
interface_method declaration;
}</pre>

<!-- wp:heading -->
<h2>Example:</h2>
<!-- /wp:heading -->

<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: #f0f0f0; color: #444444;"><span class="hljs-keyword" style="font-weight: bold;">using</span> System;
	
<span class="hljs-keyword" style="font-weight: bold;">interface</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">IDemo1</span>
{
	<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">void</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">simpleInterface</span>()</span>;
}

<span class="hljs-keyword" style="font-weight: bold;">public</span> <span class="hljs-keyword" style="font-weight: bold;">class</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">Demo</span>:<span class="hljs-title" style="color: #880000; font-weight: bold;">IDemo1</span>{
	<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">public</span> <span class="hljs-keyword" style="font-weight: bold;">void</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">simpleInterface</span>()</span>{
		Console.WriteLine(<span class="hljs-string" style="color: #880000;">"Implementation of Idemo1 interface"</span>);
	}
}
<span class="hljs-keyword" style="font-weight: bold;">public</span> <span class="hljs-keyword" style="font-weight: bold;">class</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">Program</span>
{
	<span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">public</span> <span class="hljs-keyword" style="font-weight: bold;">static</span> <span class="hljs-keyword" style="font-weight: bold;">void</span> <span class="hljs-title" style="color: #880000; font-weight: bold;">Main</span>()
	</span>{
		Demo d=<span class="hljs-keyword" style="font-weight: bold;">new</span> Demo();
		d.simpleInterface();
		Console.ReadLine();
	}
}</pre>

<!-- wp:paragraph -->
<p>Here I implement an interface called <code>IDemo1</code>. Then I use the <code>IDemo1</code> interface in <code>Demo</code> class using the operator <code>(:)</code>. Then I implement the interface method <code>simpleInterface</code> by declaring an output statement which displays Implementation IDemo1 interface.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Multiple interfaces:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To use multiple interfaces, simply use a comma between the interfaces name in class. The above program can be implemented as multiple interfaces with another interface called <code>Idemo2</code>.</p>
<!-- /wp:paragraph -->

<pre class="hljs" style="display: block; overflow-x: auto; padding: 0.5em; background: rgb(240, 240, 240); color: rgb(68, 68, 68);"><span class="hljs-keyword" style="font-weight: 700;">using</span> System;
	
<span class="hljs-keyword" style="font-weight: 700;">interface</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">IDemo1</span>
{
	<span class="hljs-function"><span class="hljs-keyword" style="font-weight: 700;">void</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">simpleInterface1</span>(<span class="hljs-params"></span>)</span>;
}

<span class="hljs-keyword" style="font-weight: 700;">interface</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">IDemo2</span>
{
	<span class="hljs-function"><span class="hljs-keyword" style="font-weight: 700;">void</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">simpleInterface2</span>(<span class="hljs-params"></span>)</span>;
}
<span class="hljs-keyword" style="font-weight: 700;">public</span> <span class="hljs-keyword" style="font-weight: 700;">class</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">Demo</span>:<span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">IDemo1</span>,<span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">IDemo2</span>{
	<span class="hljs-function"><span class="hljs-keyword" style="font-weight: 700;">public</span> <span class="hljs-keyword" style="font-weight: 700;">void</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">simpleInterface1</span>(<span class="hljs-params"></span>)</span>{
		Console.WriteLine(<span class="hljs-string" style="color: rgb(136, 0, 0);">"Implementation of Idemo1 interface"</span>);
	}
	
	<span class="hljs-function"><span class="hljs-keyword" style="font-weight: 700;">public</span> <span class="hljs-keyword" style="font-weight: 700;">void</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">simpleInterface2</span>(<span class="hljs-params"></span>)</span>{
		Console.WriteLine(<span class="hljs-string" style="color: rgb(136, 0, 0);">"Implementation of Idemo2 interface"</span>);
	}
}
<span class="hljs-keyword" style="font-weight: 700;">public</span> <span class="hljs-keyword" style="font-weight: 700;">class</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">Program</span>
{
	<span class="hljs-function"><span class="hljs-keyword" style="font-weight: 700;">public</span> <span class="hljs-keyword" style="font-weight: 700;">static</span> <span class="hljs-keyword" style="font-weight: 700;">void</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: 700;">Main</span>(<span class="hljs-params"></span>)
	</span>{
		Demo d=<span class="hljs-keyword" style="font-weight: 700;">new</span> Demo();
		d.simpleInterface1();
		d.simpleInterface2();
		Console.ReadLine();
	}
}</pre>

<!-- wp:paragraph -->
<p>From <code>C# 8.0</code> onwards we can implement a default implementation for the interface methods itself. We can also use other access specifiers such as <code>private, protected, internal, public, virtual, abstract, override, sealed, static, extern</code>. We can also implement static methods in interfaces. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For reference <a href="https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions">https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/default-interface-methods-versions</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For my other programming post -&gt; <a href="https://developersdoors.xyz/category/programming/">Go</a>.</p>
<!-- /wp:paragraph -->