Note that there are some explanatory texts on larger screens.

plurals
  1. POC# & generics - why is method in base class called instead of new method in derived class?
    primarykey
    data
    text
    <p>If the generic type argument (of either a calling class or calling method) is constrained with <code>where T : Base</code> the new method in T == Derived is not called, instead the method in Base is called. </p> <p>Why is the type T ignored for method call even though it should be known before run time?</p> <p><strong>Update</strong>: BUT, when the constraint is using an interface like <code>where T : IBase</code> the method in Base class is called (not the method in interface, which is also impossible). <br>So that means the system actually is able to detect the types that far and go beyond the type constraint! Then why doesn't it go beyond the type constraint in case of class-typed constraint? <br>Does that mean that the method in Base class that implements the interface has implicit override keyword for the method?</p> <p>Test code:</p> <pre><code>public interface IBase { void Method(); } public class Base : IBase { public void Method() { } } public class Derived : Base { public int i = 0; public new void Method() { i++; } } public class Generic&lt;T&gt; where T : Base { public void CallMethod(T obj) { obj.Method(); //calls Base.Method() } public void CallMethod2&lt;T2&gt;(T2 obj) where T2 : T { obj.Method(); //calls Base.Method() } } public class GenericWithInterfaceConstraint&lt;T&gt; where T : IBase { public void CallMethod(T obj) { obj.Method(); //calls Base.Method() } public void CallMethod2&lt;T2&gt;(T2 obj) where T2 : T { obj.Method(); //calls Base.Method() } } public class NonGeneric { public void CallMethod(Derived obj) { obj.Method(); //calls Derived.Method() } public void CallMethod2&lt;T&gt;(T obj) where T : Base { obj.Method(); //calls Base.Method() } public void CallMethod3&lt;T&gt;(T obj) where T : IBase { obj.Method(); //calls Base.Method() } } public class NewMethod { unsafe static void Main(string[] args) { Generic&lt;Derived&gt; genericObj = new Generic&lt;Derived&gt;(); GenericWithInterfaceConstraint&lt;Derived&gt; genericObj2 = new GenericWithInterfaceConstraint&lt;Derived&gt;(); NonGeneric nonGenericObj = new NonGeneric(); Derived obj = new Derived(); genericObj.CallMethod(obj); //calls Base.Method() Console.WriteLine(obj.i); genericObj.CallMethod2(obj); //calls Base.Method() Console.WriteLine(obj.i); genericObj2.CallMethod(obj); //calls Base.Method() Console.WriteLine(obj.i); genericObj2.CallMethod2(obj); //calls Base.Method() Console.WriteLine(obj.i); nonGenericObj.CallMethod(obj); //calls Derived.Method() Console.WriteLine(obj.i); nonGenericObj.CallMethod2(obj); //calls Base.Method() Console.WriteLine(obj.i); nonGenericObj.CallMethod3(obj); //calls Base.Method() Console.WriteLine(obj.i); obj.Method(); //calls Derived.Method() Console.WriteLine(obj.i); } } </code></pre> <p>Output:</p> <pre><code>0 0 0 0 1 1 1 2 </code></pre>
    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