{"id":624,"date":"2023-09-18T21:03:45","date_gmt":"2023-09-19T03:03:45","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=624"},"modified":"2023-09-18T21:09:53","modified_gmt":"2023-09-19T03:09:53","slug":"c-net-interfaces","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/c-net-interfaces\/","title":{"rendered":"C# .Net Interfaces"},"content":{"rendered":"\n<p>In C# and .NET, an interface is a programming construct that defines a contract or a set of abstract members (methods, properties, events, and indexers) that a class or a struct must implement. <\/p>\n\n\n\n<p>Interfaces provide a way to define a common set of behaviors that multiple classes can adhere to, without specifying the implementation details. <\/p>\n\n\n\n<p>Interfaces are a fundamental part of object-oriented programming and are used for achieving abstraction, polymorphism, and code reusability.<\/p>\n\n\n\n<p>Some of the key points about interfaces in C# and .NET, as follows:<\/p>\n\n\n\n<ol>\n<li><strong>Declaration<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>You define an interface using the <code>interface<\/code> keyword, followed by the interface name and a set of member declarations.<\/li>\n\n\n\n<li>Members declared in an interface do not have implementations; they only declare the method signatures, properties, events, or indexers.<\/li>\n<\/ul>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-php\">interface IExampleInterface\n   {\n       void Method1();\n       int Property1 { get; set; }\n       event EventHandler MyEvent;\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"2\">\n<li><strong>Implementing an Interface<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>A class or struct can implement an interface by providing concrete implementations for all the members declared in the interface.<\/li>\n\n\n\n<li>The <code>:<\/code>, followed by the interface name, is used to indicate interface implementation.<\/li>\n<\/ul>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#FB8CFF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">class MyClass : IExampleInterface\n   {\n       public void Method1()\n       {\n           \/\/ Provide implementation for Method1\n       }\n\n       public int Property1\n       {\n           get { return 42; }\n           set { \/* Provide implementation for setter *\/ }\n       }\n\n       public event EventHandler MyEvent;\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"3\">\n<li><strong>Multiple Interface Implementation<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>A class can implement multiple interfaces, separated by commas.<\/li>\n<\/ul>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#C9D8B6\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">class MyDerivedClass : IExampleInterface, IAnotherInterface\n   {\n       \/\/ Provide implementations for members of both interfaces\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"4\">\n<li><strong>Interface Inheritance<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>Interfaces can inherit from other interfaces. An interface can inherit members from one or more base interfaces.<\/li>\n<\/ul>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#C9D8B6\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">interface IDerivedInterface : IBaseInterface\n   {\n       \/\/ Members of IDerivedInterface\n   }<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"5\">\n<li><strong>Polymorphism<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>Interfaces enable polymorphism, allowing objects of different classes that implement the same interface to be treated interchangeably.<\/li>\n<\/ul>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#D4F8FC\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-clike\">   IExampleInterface exampleObject = new MyClass();\n   exampleObject.Method1(); \/\/ Calls Method1 of MyClass<\/code><\/pre><\/div><\/div>\n\n\n\n<ol start=\"6\">\n<li><strong>Abstraction and Contracts<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>Interfaces define a contract that classes must adhere to, promoting a level of abstraction in software design.<\/li>\n\n\n\n<li>They allow you to work with objects based on their common behaviors without concern for their concrete types.<\/li>\n<\/ul>\n\n\n\n<ol start=\"6\">\n<li><strong>API Design<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul>\n<li>Interfaces are commonly used in API design to define the expected behavior of classes or services, making it easier to create pluggable and interchangeable components.<\/li>\n<\/ul>\n\n\n\n<p>Interfaces are a powerful tool in C# and .NET for achieving abstraction, creating flexible and extensible code, and enabling polymorphism. <\/p>\n\n\n\n<p>They are a key part of many design patterns and are widely used in object-oriented programming.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C# and .NET, an interface is a programming construct that defines a contract or a set of abstract members (methods, properties, events, and indexers) that a class or a struct must implement. Interfaces provide a way to define a common set of behaviors that multiple classes can adhere to, without specifying the implementation details. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":470,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,51,32,52],"tags":[53,115,54,100,121],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/624"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=624"}],"version-history":[{"count":5,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/624\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/624\/revisions\/630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/470"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}