6

Just curious about how .NET CLR handles interfaces internally?

Q1] What happens when CLR encounters something like :

simple interface example. (same used below.)

interface ISampleInterface
    {
        void SampleMethod();
    }

    class ImplementationClass : ISampleInterface
    {
        // Explicit interface member implementation: 
        public void SampleMethod()
        {
            // Method implementation.

        }

        static void Main()
        {
            //Declare an interface instance.
            ISampleInterface mySampleIntobj = new ImplementationClass();  // (A)
           // Call the member.
            mySampleIntobj.SampleMethod();

            // Declare an interface instance. 
            ImplementationClass myClassObj = new ImplementationClass();  // (B)
           //Call the member.
            myClassObj.SampleMethod();

        }
    }

Q2 : In the above example how are (A) and (B) differentiated ?

Q3 : Are Generic Interfaces treated differently?

(Feel like a noob when asking basic questions like these ...anyways....)

Thx all.

Amitd
  • 4,769
  • 8
  • 56
  • 82
  • since it's only partly an answer I'll leave it here. To start off with in your code mySampleIntobj.SampleMethod(); and myClassObj.SampleMethod(); might call two different methods. The first one will call any explicitly implemented methods matching the call, the second will not. only in the case where there's no explicitly method matching will they call the same method – Rune FS Jul 16 '10 at 07:00
  • Does this compile? AFAIK, you should have a public `SampleMethod()` for the (B) call. – Humberto Jul 16 '10 at 13:56
  • oh yes sorry..edited/corrected now :) thx :) – Amitd Jul 16 '10 at 16:10

2 Answers2

2

There are practically no differences in those bits of code. Both end up calling the same function. There may be minor performance benefits in calling the method through the class type.

If you want to how these stuff are implemented, have a look at Virtual Method Tables.

For deeper information, see this.

Gorkem Pacaci
  • 1,741
  • 1
  • 11
  • 9
  • read it thrice and still not getting it :) ..i keep trying ;) – Amitd Jul 20 '10 at 05:39
  • The second link (the one to the MSDN article) is broken because Microsoft no longer publishes old MSDN articles in web viewable format. They still have the files downloadable though. Does someone know what edition and article that was a link to? – Jeff Walker Code Ranger Jun 23 '16 at 17:10
  • It's silly, as if they don't care at all about the legacy code. – Gorkem Pacaci Jun 25 '16 at 21:21
-5

Using the interface while creating the object reference is considered a better practice as compared to directly instanciating it with a class type. This comes under the programming to an interface principle.

This means you can change the concrete class which implements the same interface at runtime using something like dependency injection or even may be reflection. Your code will not be required to be changed if you program to an interface as compared to programming to an concrete type.

Nilesh Gule
  • 1,511
  • 12
  • 13