Note that there are some explanatory texts on larger screens.

plurals
  1. POWrong overload is overridden when two methods have identical signatures after substitution of type arguments
    primarykey
    data
    text
    <p>We believe this example exhibits a bug in the C# compiler (do make fun of me if we are wrong). This bug may be well-known: After all, our example is a simple modification of what is described <a href="http://blogs.msdn.com/b/samng/archive/2008/02/13/generic-method-substitutions-and-unification.aspx" rel="noreferrer">in this blog post</a>.</p> <pre><code>using System; namespace GenericConflict { class Base&lt;T, S&gt; { public virtual int Foo(T t) { return 1; } public virtual int Foo(S s) { return 2; } public int CallFooOfT(T t) { return Foo(t); } public int CallFooOfS(S s) { return Foo(s); } } class Intermediate&lt;T, S&gt; : Base&lt;T, S&gt; { public override int Foo(T t) { return 11; } } class Conflict : Intermediate&lt;string, string&gt; { public override int Foo(string t) { return 101; } } static class Program { static void Main() { var conflict = new Conflict(); Console.WriteLine(conflict.CallFooOfT("Hello mum")); Console.WriteLine(conflict.CallFooOfS("Hello mum")); } } } </code></pre> <p>The idea is simply to create a class <code>Base&lt;T, S&gt;</code> with two virtual methods whose signatures will become identical after an 'evil' choice of <code>T</code> and <code>S</code>. The class <code>Conflict</code> overloads only one of the virtual methods, and because of the existence of <code>Intermediate&lt;,&gt;</code>, it should be well-defined which one!</p> <p>But when the program is run, the output seems to show that the wrong overload was overridden.</p> <p>When we read Sam Ng's <a href="http://blogs.msdn.com/b/samng/archive/2008/03/04/generic-method-substitutions-and-unification-part-two.aspx" rel="noreferrer">follow-up post</a> we get the expression that that bug was not fixed because they believed a type-load exception would always be thrown. But in our example the code compiles and runs with no errors (just unexpected output).</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.
 

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