title: C# Hiding, overriding and calling function from base class
body: <p>I'm learning C# and I encountered the following problem. I have two classes: base and derived:</p>
  
  <pre><code>class MyBase
  {
      public void MyMethod()
      {
          Console.WriteLine("MyBase::MyMethod()");
      }
  }
  
  
  class MyDerived: MyBase
  {
      public void MyMethod()
      {
          Console.WriteLine("MyDerived::MyMethod()");
      }
  }
  </code></pre>
  
  <p>For now, without <code>virtual</code> and <code>override</code> key words. When I compile this I get the warning (which is of course expected) that I try to hide <code>MyMethod</code> from <code>MyBase</code> class.</p>
  
  <p>What I want to do is to call the method from the base class having an instance of derived class. I do this like this:</p>
  
  <pre><code>MyDerived myDerived = new MyDerived();
  ((MyBase)myDerived).MyMethod();
  </code></pre>
  
  <p>It works fine when I do not specify any <code>virtual</code>, etc. keywords in the methods. I tried to put combination of the keywords and I got the following results:</p>
  
  <pre><code>| MyBase::MyMethod | MyDerived::MyMethod | Result printed on the console |
  | -----------------|---------------------|-------------------------------|
  | -                | -                   | MyBase::MyMethod()            |
  | -                | new                 | MyBase::MyMethod()            |
  | virtual          | new                 | MyBase::MyMethod()            |
  | virtual          | override            | MyDerived::MyMethod()         |
  </code></pre>
  
  <p>I hope the table is clear to you. <strong>I have two questions</strong>:</p>
  
  <ol>
  <li>Is it the correct way to call the function from the base class (<code>((MyBase)myDerived).MyMethod();</code>)? I know about <code>base</code> keyword, but it can be called only from the inside of the derived class. Is it right?</li>
  <li>Why in the last case (with <code>virtual</code> and <code>override</code> modifiers) the method which was called came from the derived class? Would you please explain that?</li>
  </ol>