{"id":432,"date":"2022-01-09T08:34:51","date_gmt":"2022-01-09T01:34:51","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=432"},"modified":"2023-04-02T10:33:13","modified_gmt":"2023-04-02T03:33:13","slug":"csharp-interface","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-interface\/","title":{"rendered":"C# Interface"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the C# interface and how to use the interfaces in programming to make the application more extensible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# interface<\/h2>\n\n\n\n<p>An interface defines a contract between <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-class\/\">classes<\/a>. To define an interface, you use the <code>interface<\/code> keyword followed by the interface name like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">InterfaceName<\/span>\n{\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>By convention, interface names in C# start with the letter <code>I<\/code> in uppercase like <code>IReadable<\/code> and <code>IWritable<\/code>. Like a class, an interface can contain the following members:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Methods<\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-property\/\">Properties<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/c-indexer\/\">Indexers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-events\/\">Events<\/a><\/li>\n<\/ul>\n\n\n\n<p>But unlike a class, the members of an interface only have declarations. In other words, they do not contain implementations. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">IMyInterface<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">MyMethod<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>All the members of an interface are <code>public<\/code> by default. Therefore, you don&#8217;t need to use the <code>public<\/code> keyword for the interface&#8217;s members.<\/p>\n\n\n\n<p class=\"note\">Note that starting with C# 8 or later, interface members can have <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-interface-default-implementation\/\">default implementations<\/a>.<\/p>\n\n\n\n<p>Like an <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-abstract-class\/\">abstract class<\/a>, you cannot create a new instance of an interface. To use an interface, you need to implement it from your <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-class\/\">class<\/a>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span>: <span class=\"hljs-title\">IMyInterface<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">MyMethod<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-comment\">\/\/ implementation<\/span>\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we define the <code>MyClass<\/code> that implements the <code>IMyInterface<\/code> interface. The syntax used for implementing an interface is like the syntax used for <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-inheritance\/\" target=\"_blank\" rel=\"noreferrer noopener\">inheriting from a class<\/a>.<\/p>\n\n\n\n<p>In this example, we define the <code>MyClass<\/code> that implements the <code>IMyInterface<\/code> interface. The access modifier of the <code>MyMethod()<\/code> is public.<\/p>\n\n\n\n<p>The class that implements an interface need to provide all the implementation of the interface members.<\/p>\n\n\n\n<p>A class can inherit from a single class but can implement multiple interfaces. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">MyClass<\/span> : <span class=\"hljs-title\">IMyInterface1<\/span>, <span class=\"hljs-title\">IMyInterface2<\/span>\n{\n   <span class=\"hljs-comment\">\/\/...<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">C# interface example<\/h2>\n\n\n\n<p>The following example defines a simple interface called <code>IReadable<\/code> with one method <code>Read()<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">IReadable<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">Read<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following defines the <code>ConsoleReader<\/code> class that implements the <code>IReadable<\/code> interface:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ConsoleReader<\/span> : <span class=\"hljs-title\">IReadable<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">Read<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> Console.ReadLine() ?? <span class=\"hljs-keyword\">string<\/span>.Empty;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because the <code>ConsoleReader<\/code> class implements the <code>IReadable<\/code> interface, it must implement the <code>Read()<\/code> method. This is called design by contract.<\/p>\n\n\n\n<p>The following creates a new instance of the <code>ConsoleReader<\/code> class and read a string from the console:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Program.cs<\/span>\n\nIReadable reader = <span class=\"hljs-keyword\">new<\/span> ConsoleReader();\n<span class=\"hljs-keyword\">string<\/span> input = reader.Read();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you want to <a href=\"https:\/\/csharptutorial.net\/csharp-file\/csharp-read-text-files\/\">read the contents of a text file<\/a>, you can define a new class <code>FileReader<\/code> that implements the <code>IReadable<\/code> interface:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">FileReader<\/span> : <span class=\"hljs-title\">IReadable<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> Filename { <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>; }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">FileReader<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> filename<\/span>)<\/span>\n    {\n        Filename = filename;\n    }\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">Read<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> File.ReadAllText(Filename);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>FileReader<\/code> class has the <code>Filename<\/code> as property and implements the <code>Read()<\/code> method of the IReadable interface, which reads the contents of a text file specified by the <code>Filename<\/code>.<\/p>\n\n\n\n<p>The following creates a new instance of the <code>FileReader<\/code> class and reads all contents of the file located at <code>C:\\temp\\test.txt<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Program.cs<\/span>\nIReadable reader = <span class=\"hljs-keyword\">new<\/span> FileReader(<span class=\"hljs-string\">@\"C:\\temp\\test.txt\"<\/span>);\n<span class=\"hljs-keyword\">string<\/span> content = reader.Read();\n\nConsole.WriteLine(content);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Both the <code>ConsoleReader<\/code> and <code>FileReader<\/code> classes implement the <code>IReadable<\/code> interface. It means that you can easily swap them. For example, the following defines a method that accepts an interface <code>IReadable<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">UppercaseReader<\/span>(<span class=\"hljs-params\">IReadable reader<\/span>)<\/span>\n{\n    <span class=\"hljs-keyword\">return<\/span> reader.Read().ToUpper();\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>UppercaseReader()<\/code> function calls the <code>Read()<\/code> method and returns the string in uppercase.<\/p>\n\n\n\n<p>When calling the <code>UppercaseReader()<\/code> function, you can pass an instance of any class that implements the <code>IReadable<\/code> interface. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Program.cs<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-title\">UppercaseReader<\/span>(<span class=\"hljs-params\">IReadable reader<\/span>)<\/span>\n{\n    <span class=\"hljs-keyword\">return<\/span> reader.Read().ToUpper();\n}\n\nIReadable&#91;] readers = { \n    <span class=\"hljs-keyword\">new<\/span> ConsoleReader(),\n    <span class=\"hljs-keyword\">new<\/span> FileReader(<span class=\"hljs-string\">@\"C:\\temp\\test.txt\"<\/span>) \n};\n\n<span class=\"hljs-keyword\">foreach<\/span> (<span class=\"hljs-keyword\">var<\/span> reader <span class=\"hljs-keyword\">in<\/span> readers)\n{\n    Console.WriteLine(UppercaseReader(reader));\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we create an <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-array\/\">array<\/a> that contains two instances of classes that implement the <code>IReadable<\/code> interface and pass them to the <code>UppercaseReader()<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An interface is a contract between classes.<\/li>\n\n\n\n<li>By convention, an interface name starts with the letter <code>I<\/code>.<\/li>\n\n\n\n<li>An interface contains members including methods, properties, indexers, and events without implementations.<\/li>\n\n\n\n<li>A class that implements an interface must provide implementations for the members of the interface.<\/li>\n\n\n\n<li>A class can implement multiple interfaces.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"432\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-interface\/\"\n\t\t\t\tdata-post-title=\"C# Interface\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"432\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-interface\/\"\n\t\t\t\tdata-post-title=\"C# Interface\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the C# interface and how to use the interfaces in programming to make the application more extensible.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":50,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-432","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=432"}],"version-history":[{"count":4,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/432\/revisions"}],"predecessor-version":[{"id":1349,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/432\/revisions\/1349"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}