Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This happens because you are not overloading the method and nor is the base method declared virtual, so virtual method dispatch is not used. This means that method calls will be bound at compile-time, and so <em>type of the reference</em> used to call the method will determine which method is called, instead of the <em>type of the referenced object</em>.</p> <p>You should get a warning on the line declaring <code>foo()</code> within <code>Specific</code> since it hides the member from the parent type and you have not specified <code>new</code>. Pay attention to warnings!</p> <p>To resolve this issue, declare the method in <code>Generic</code> as <code>virtual</code>:</p> <pre><code>public virtual String foo(M n) </code></pre> <p>And override it in <code>Specific</code>:</p> <pre><code>public override String foo(M n) </code></pre> <p>In this case, the call will be bound to a specific override of this method at runtime, using the vtable of the actual object the method is invoked on.</p> <p>Note that an override can't change the types of parameters of the method being overridden, so you cannot declare the <code>foo()</code> in <code>Specific</code> to accept an <code>N</code> object. However, you can specify an additional <em>overload</em> if desired. The <code>foo(M)</code> overload may need to look at the type of the passed object to determine what to do with it. (Or, even better, make <code>foo(M)</code> <em>not care</em> if it was passed an <code>M</code> or an <code>N</code>.)</p> <p>Possible implementation of an override that should only accept <code>N</code> (though this smells of bad design):</p> <pre><code>public override String foo(M m) { if (m == null) { throw new ArgumentNullException("m"); } N n = m as N; if (n == null) { throw new ArgumentException("Must be an N instance.", "m"); } return foo(n); } public virtual String foo(N n) { // Use the n variable } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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