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.

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 method Commented Jul 16, 2010 at 7:00
  • Does this compile? AFAIK, you should have a public SampleMethod() for the (B) call. Commented Jul 16, 2010 at 13:56
  • oh yes sorry..edited/corrected now :) thx :) Commented Jul 16, 2010 at 16:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

read it thrice and still not getting it :) ..i keep trying ;)
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?
It's silly, as if they don't care at all about the legacy code.
-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.

2 Comments

This seems better explanation of programming to an interface stackoverflow.com/questions/1848442/…
Mostly correct, but off-topic. Doesn't answer the question. Downvoted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.