Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's first make sure that I am interpreting your question correctly. You have classes defined as above. You are instantiating an instance of <code>A</code> and invoking a method <code>example</code> that <code>A</code> inherits from the base class. You want to know if it's possible for the call <code>this.AsAString()</code> in the method <code>Letter.Example</code> to invoke the base implementation of <code>AsAString</code> rather than the derived implementation.</p> <p>First, let's understand why with <code>example</code> defined as above, invoking <code>Letter.example</code> via an instance of <code>A</code> (e.g., <code>new A().example</code>) will cause <code>A.AsAString</code> to be invoked. From the specification (§7.4.4):</p> <blockquote> <p>The function member implementation to invoke is determined:</p> <p>If the compile-time type of E is an interface, the function member to invoke is the implementation of M provided by the run-time type of the instance referenced by E. This function member is determined by applying the interface mapping rules (§13.4.4) to determine the implementation of M provided by the run-time type of the instance referenced by E.</p> <p>Otherwise, <strong>if M is a virtual function member, the function member to invoke is the implementation of M provided by the run-time type of the instance referenced by E. This function member is determined by applying the rules for determining the most derived implementation (§10.6.3) of M with respect to the run-time type of the instance referenced by E.</strong></p> <p>Otherwise, M is a non-virtual function member, and the function member to invoke is M itself.</p> </blockquote> <p>So now let's consider your situation. You have an instance <code>a</code> of a class <code>A</code> that derives from <code>Letter</code>. You have invoked a method named <code>example</code> via the syntax <code>a.example()</code>. This will invoke <code>Letter.example</code> which has definition:</p> <pre><code>public void example() { this.AsAString(); } </code></pre> <p>This will invoke <code>Letter.AsAString</code>. But, <code>Letter.AsAString</code> is declared <code>virtual</code> and therefore, by the bolded rule above, the method that is invoked is <code>A.AsAString</code> because <code>this</code> is of type <code>A</code>, <code>A</code> derives from <code>Letter</code>, and <code>A</code> provides an <code>override</code> of <code>Letter.AsAString</code>.</p> <p>Now, if you change the definition of <code>A.AsAString</code> so that it hides the base implementation using the <code>new</code> modifier</p> <pre><code>public new void AsAString() { Console.WriteLine("A"); } </code></pre> <p>then <code>a.example</code> will cause the base implementation to be used and you will see the output <code>???</code> as you desire. This is because, by the rule above, the most derived implementation of <code>Letter.AsAString</code> (i.e., the most derived type in the hierarchy of <code>A</code> that provides a definition of the <code>virtual</code> method <code>AsAString</code>) is the base implementation. The <code>new</code> modifier allows <code>A</code> to have a method named <code>AsAString</code> with the same signature as <code>Letter.AsAString</code> but it is not a <code>virtual</code> method.</p> <p>Please let me know if I am interpreting your question incorrectly, or if any of the above requires clarification.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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