C# · CodeProject · Dotnet

Assumed mistake in Explicit Interface implementation

Dear Friends,

Again, i wont be discussing anything about Explicit interface implementation feature in c# here, i am sure either you know about it or there are many great articles on web you can take a look at.

But i am must say that you must read the excellent article by my friend Abhiseksur on internals of Explicit interface implementation feature in C#

Let me outline particular point from Abhisek’s article which i or this article is interested in:

  • Explicit method is always implicitly private to its type
  • You can’t modify/specify any modifier to Explicit method.

So that means we cant do this as shown below:

  1.    interface IMyInterface
  2.     {
  3.         void InterfaceMethod();
  4.     }
  5.     class MyClass : IMyInterface
  6.     {
  7.         void IMyInterface.InterfaceMethod()
  8.         {
  9.         }
  10.     }
  11.     new MyClass().InterfaceMethod() //Compiler Error, since InterfaceMethod is private to MyClass.

So now any developer can be relaxed about this explicit implementation method that his code is fail proof since its private and no body can corrupt it. Yes logically at least it looks like that. But my curiousness doesn’t end here. Now lets see how safe is this Explicit implementation method in this class.

Assume the below code:

  1.    abstract class MyAbstractClass
  2.     {
  3.         public int MyValue { get; set; }
  4.     }
  5.     interface IMyInterface
  6.     {
  7.         void InterfaceMethod(MyAbstractClass absClassObj);
  8.     }
  9.     class MyClass : IMyInterface
  10.     {
  11.         void IMyInterface.InterfaceMethod(MyAbstractClass absClassObj)
  12.         {
  13.             var value = absClassObj.MyValue;
  14.         }
  15.         public void SomeOtherMethod()
  16.         {
  17.         }
  18.     }
  19.     class SomeEvilClass
  20.     {
  21.         MyClass myclass = new MyClass();
  22.         public void SomeEvilMethod()
  23.         {
  24.             (myclass as IMyInterface).InterfaceMethod(null);
  25.         }
  26.     }

This above code crashes the application. Although it’s not a great catch what i am speculating here, but could be a bit of flaw if the developer fails to over see it. I have no idea where and when this feature we make use off. But the workaround I am thinking off is to keep interface internal, at least you are sure that your assembly code is not evil and you can control it some how but on flip side it wont pay off much if i have interface which is not public.

That’s all friends, please leave comments for my learning’s 🙂



2 thoughts on “Assumed mistake in Explicit Interface implementation

  1. Well, as I have already told you, when you are using the reference of Interface in your case
    (myclass as IMyInterface) you will get the method as public. Its in the implementation detail. It is private to all other references but the interface itself. I think it is weird, but this flexibility is provided into the C# language.

    But I must say its a good call from you and BTW, nice to see you started blogging. 🙂 Like your blog layout and design and also name of the blog. Cool Keep it up.

    1. Thanks and yes i know its one way is good but other way when forgotten its a pain in the back.
      Thanks and you guys are source of inspiration for blogging. 🙂

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s