Note that there are some explanatory texts on larger screens.

plurals
  1. POExplicit interface implementation cannot be virtual
    primarykey
    data
    text
    <p>For the record, I've already seen this <a href="https://connect.microsoft.com/VisualStudio/feedback/details/93163/allow-explicit-interface-implementations-to-be-virtual-and-to-be-called-from-derived-classes">connect item</a> but I can't really understand what would be the problem in supporting this.</p> <p>Say I have the following code:</p> <pre><code>public interface IInterface { void Method(); } public class Base : IInterface { virtual void IInterface.Method() { throw new NotImplementedException(); } } </code></pre> <p>what is the problem with the virtual identifier? Having a virtual modifier would make it possible to <code>override</code> indicating there's a different implementation in the base class. I can make it work now by removing the virtual method and creating the derived class like this:</p> <pre><code>public class Derived : IInterface { void IInterface.Method() { throw new NotImplementedException(); } } </code></pre> <p>however this way I've really no indication at all that I'm overriding something. </p> <p><strong>Update:</strong><br> According to the C# (part: 20.4.1 Explicit interface member implementations) spec there are 2 reasons. </p> <ol> <li>Hiding of certain methods (which I'm using it for). </li> <li>Having 2 functions with the same signature but different return types (usefull for IClonable for example).</li> </ol> <p>It doesn't say anything however about why you can't make these methods virtual.</p> <p><strong>Update2:</strong><br> Given the answers I think I should rephrase the real question here. If The above 2 reasons are the reason why explicit implementation of interfaces was made possible in the first place. Why would it be a problem if you make a method virtual. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload