1

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.

I've some stuff on the internet, some stuff here, and the explanation that made most sense for me was this - How will I know when to create an interface?.

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 :

you have a, b, c, d of 4 different types. all over your code you have something like:

a.Process();
b.Process();
c.Process();
d.Process();

Why not have them implement IProcessable, and then do

List<IProcessable> list;

foreach(IProcessable p in list)
    p.Process();

This will scale much better when you add, say, 50 types of classes that all do the same thing.

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 - a, b, c, d. All those classes are the same :

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");
        }
    }
}

Just 4 different names and the method Test() printing the name. Here is the interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    interface ITest
    {
        public void Test();
    }
}

And by what I understand in my Program.cs I have this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        List<ITest> list;
        static void Main(string[] args)
        {
            foreach (ITest p in list)
                p.Test();
        }
    }
}

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 Main() method above. However I get the following error:

An object reference is required for the non-static field, method, or property

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.

Community
  • 1
  • 1
Leron
  • 9,546
  • 35
  • 156
  • 257
  • 3
    The interfaces aren't the problem here -- it's the `list` field of `Program`. That needs to be a static field to use it from the static method `Main`. (You'll probably also want to put some things in that list) – kevingessner Feb 26 '13 at 19:28
  • 1
    This would be a good read - http://stackoverflow.com/q/14482483/218882 – Hari Pachuveetil Feb 26 '13 at 19:30

4 Answers4

3

The issue here has nothing to do with the interface but with the class Program. The field list is an instance member, while the method Main is static. As the exception says, you cannot reference non-static members from static methods.

Additionally, list was never instantiated or initialized (at least not within the snippet you posted). So even list if was declared as static, your code would've produced a NullReferenceException.

Try changing your Program class to this:

class Program
{
    static List<ITest> list;
    static void Main(string[] args)
    {
        list = new List<ITest>();
        // ...
        // initialize list 
        // ...

        foreach (ITest p in list)
            p.Test();
    }
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
1

Regarding your error, it looks like list is not instantiated, and it's an instance field, being accessed from a static method.


Interfaces aren't for inheritance, they're for polymorphism.

An interface isn't for saving you code. If you need to do that, use Abstract classes.

All an interface does is guarantee to the compiler that your class will behave in a particular way. It doesn't specify anything about implementation.

For instance, you have both List and LinkedList are implimented differently, but they both conform to the IEnumberable interface and can therefore be iterated over in a the foreach loop

1

Your code looks good in terms of creating classes that will implement the interface. The problem seems to lie in your initialization.

As you declare it, List<ITest> list is not a static field. The Main function is a static method, so it can only access other static fields.

If you want to proceed with this basic example, try setting list as a static field, or as a variable within Main, and instantiating it, populating it with some objects before using it in your foreach loop.

Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
0

the usage of an interface is defining something like a placeholder for an object, that will definitely have some specific behavior without actually knowing it. For example you can have class A and class B, both of them implement IMyInterface. If you need an instance of IMyInterface you can use both of them and call a method specified in interface but implemented in A or B. Also there is great convention-based usage of interfaces in IoC containers.

HardLuck
  • 1,497
  • 1
  • 22
  • 43