Note that there are some explanatory texts on larger screens.

plurals
  1. POExplicit interface implementation for classes in C#
    text
    copied!<p>Is there something comparable to explicit interface implementation but for classes instead in C#?</p> <p>Consider the following situation:</p> <p>Company X provides a library containing a class as follows:</p> <pre><code>public class LibraryClass { public virtual void A() { } public void DoWork() { // does something } } </code></pre> <p>Company Y uses this library in one of its products and inherits from <code>LibraryClass</code>:</p> <pre><code>public class UserClass : LibraryClass { public virtual void B() { } } </code></pre> <p>So far everything works fine. But at someday X releases a new library version and adds a virtual method <code>B()</code>to <code>LibraryClass</code>:</p> <pre><code>public class LibraryClass { public virtual void A() { } public virtual void B() { } public void DoWork() { // does something // now uses B with certain semantic assumptions } } </code></pre> <p>Now Y updates to the new library version. While compiling with a reference to the new version, the compiler emits a warning saying that <code>UserClass.B()</code> is hiding the inherited method <code>LibraryClass.B()</code> and therefore should either specify the <code>new</code> keyword or override the method. Because there is a semantic gap between the existing method <code>UserClass.B()</code> and the newly introduced method <code>LibraryClass.B()</code> Y decides to introduce the <code>new</code> keyword because any existing override of <code>UserClass.B()</code> will probably not provide the semantics expected by <code>DoWork()</code> which would break the code. On the other hand Y wants to use a new feature of the library which would require an override of <code>LibraryClass.B()</code>. Now this is not possible: If the override would be done in a derived class of <code>UserClass</code> the override would refer to <code>UserClass.B()</code> due to the <code>new</code> keyword; an override of <code>B</code> in <code>UserClass</code> itself is not even allowed as it already defines a public method with that signature.</p> <p>This situation could be solved if there was either a way in a derived class of <code>UserClass</code> to specify that the override refers to <code>LibraryClass.B()</code> which is not possible as far as I know -or- if <code>B()</code> could be explicitly overriden in <code>UserClass</code>:</p> <pre><code>public class UserClass : LibraryClass { ... // Override this method in order to change the behavior of LibraryClass.B() public virtual void LibraryB() { } private void override LibraryClass.B() { LibraryB(); } ... } </code></pre> <p>Is there any way in the language to solve this situation other than renaming the original <code>B()</code> in <code>UserClass</code> (which might not even be possible if it was part of a library itself which is consumed by company Z)? If not, is this a C# limitation or a limitation of the CLR?</p> <p>Sorry for the long post and thank you for reading up to this point.</p> <p><strong>Edit</strong>: This is not a CLR limitation. C++/CLI supports named overrides which solve the situation, so you could do something like <code>virtual void LibraryB(void) = LibraryClass::B;</code>. The C# design team probably just missed this issue.</p>
 

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