From the understanding, the virtual keyword enables you to employ the bottom class' method, and override enables you to definitely override it inside a class that gets in the base class. My confusion is the fact that I simply examined not while using virtual keyword within the base class' method definition and never including that method within the derived class, and that i was still being in a position to call the bottom class' method (it turned up in intellisense and went). Also, I understand when i needed a brand new method definition for your same way of my derived class which i can use the brand new keyword..

I'm most likely missing an important factor but this is one way I realize it and that's why I'm confused regarding the reason for virtual and override

My confusion is the fact that I simply examined not while using virtual keyword in the bottom class' method definition and never including that method within the derived class, and that i was still being in a position to call the bottom class' method (it turned up in intellisense and went).

This really is expected behavior.

Virtual enables you to definitely override a technique that is defined within the base class, quite simply extend the implementation from the method within the derived class.

Variations between your new keyword and also the override one can be found on MSDN here.

It's access modifiers (private, public, protected) that affects if you can to call the bottom class method within the derived class or otherwise.

Virtual enables you to definitely override the bottom class method. You are able to still call the technique without them and can not have the ability to override the technique without them.

Sorry, I didn't remember to say polymorphism. If you possess a base class that defines a technique that you can do something similar to this.

The below snippet prints from B

public class A
{
  virtual void foo
  {
    Console.WriteLine("From A");
  }
}

public class B : A
{
  override void foo()
  {
   Console.WriteLine("From B");
  }
}

static void Main(string[] args) 
{
   A testA = new B();

   a.foo();
}

To override a technique inside a base class the technique you're overriding should be marked as virtual - you override a technique while using override key phrase.

new poor a technique is slightly different. It enables you to definitely give a method inside a subclass with similar title like a method within the base class. Which method will get known as will rely on the reference that you employ to gain access to the technique.

You have access to any techniques implemented within the base class (susceptible to access modifiers) no matter whether or not they are marked virtual or otherwise.

Whenever you mark a technique as virtual or abstract, run-time type checking can be used to look for the actual implementation from the approach to call: it inspections for that "most derived type" that implements/overrides the virtual method, and creates that implementation. An ordinary method doesn't need run-time type checking.

Thus, the excellence between virtual and non-virtual includes a significant effect on the semantics of the code, specifically for public APIs. If you notice this:

class A { void foo(); virtual void bar(); }
...
A myObject = getSomeObject();
myObject.foo();
myObject.bar();

You can be certain the implementation of foo that's invoked may be the one declared at school A. The reason being only compile-time type checking can be used to look for the approach to be known as. However, the particular implementation of bar that's invoked might be the implementation in certain subclass of A, with respect to the run-time kind of myObject.

More information: http://msdn.microsoft.com/en-us/library/aa645767(v=vs.71).aspx