Note that there are some explanatory texts on larger screens.

plurals
  1. POExplanation of Interface usage (C# perspective)
    primarykey
    data
    text
    <p>I'm working with C# and .NET for some time now and even though I've been working with OOP languages before (Java to be more precise), while working on .NET applications I see interfaces used a lot more than I've seen in Java. I don't have a lot of experience at all so I'm not saying that interfaces are more used in .NET but now I felt the need to get deeper understanding of interfaces and what are the benefits which make people to use them so often.</p> <p>I've some stuff on the internet, some stuff here, and the explanation that made most sense for me was this - <a href="https://stackoverflow.com/questions/444245/how-will-i-know-when-to-create-an-interface">How will I know when to create an interface?</a>. </p> <p>And here comes my question. I don't expect some magical explanation of interfaces at all, cause I've read enough to see that there is not such thing. I hope that with experience and coding the deeper understanding will come by itself so I decided to try the example pointed as benefit of using the interfaces from the accepted answer in the link. I'll copy-paste it here too :</p> <p>you have a, b, c, d of 4 different types. all over your code you have something like:</p> <pre><code>a.Process(); b.Process(); c.Process(); d.Process(); </code></pre> <p>Why not have them implement <code>IProcessable</code>, and then do</p> <pre><code>List&lt;IProcessable&gt; list; foreach(IProcessable p in list) p.Process(); </code></pre> <p>This will scale much better when you add, say, 50 types of classes that all do the same thing.</p> <p>The problem is that it seems I haven't got this. After I read it I open my Visual Studio 2010 and tried to recreate the example like this - I made a project and created four classes - <code>a</code>, <code>b</code>, <code>c</code>, <code>d</code>. All those classes are the same :</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class a : ITest { public void Test() { System.Console.WriteLine("Hi from a"); } } } </code></pre> <p>Just 4 different names and the method <code>Test()</code> printing the name. Here is the interface:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { interface ITest { public void Test(); } } </code></pre> <p>And by what I understand in my <code>Program.cs</code> I have this:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { List&lt;ITest&gt; list; static void Main(string[] args) { foreach (ITest p in list) p.Test(); } } } </code></pre> <p>Well. It seems that I really got the things wrong. I thought that by implementing the same interface I can automatically get a list of all classes and use them, for example like in my <code>Main()</code> method above. However I get the following error:</p> <blockquote> <p><em>An object reference is required for the non-static field, method, or property</em></p> </blockquote> <p>Well, can someone lower the level to me and explain me in terms of this example how I exactly benefit like this by using an interface.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload