How does .Net CLR implement an "Interface" internally?
Asked Answered
P

2

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.

Paedogenesis answered 16/7, 2010 at 6:30 Comment(3)
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 methodDrumhead
Does this compile? AFAIK, you should have a public SampleMethod() for the (B) call.Lempres
oh yes sorry..edited/corrected now :) thx :)Paedogenesis
C
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.

Couturier answered 16/7, 2010 at 13:51 Comment(3)
read it thrice and still not getting it :) ..i keep trying ;)Paedogenesis
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?Trueblood
It's silly, as if they don't care at all about the legacy code.Couturier
H
-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.

Heptamerous answered 18/7, 2010 at 14:19 Comment(2)
This seems better explanation of programming to an interface #1848942Paedogenesis
Mostly correct, but off-topic. Doesn't answer the question. Downvoted.Bungalow

© 2022 - 2024 — McMap. All rights reserved.