A simple case:
public class Foo { public /*virtual*/ bool DoSomething() { return false; } } public class Bar : Foo { public /*override or new*/ bool DoSomething() { return true; } }
Call the code like this:
Foo a = new Bar(); a.DoSomething();
NOTE: The important thing is that our object is actually a Bar
, but we are storing it in a variable of type Foo
(this is similar to casting it)
Explain:
New keyword is for Hiding. - means you are hiding your method at runtime. Output will be based base class method.
Override for overriding. - means you are invoking your derived class method with the reference of base class. Output will be based on derived class method.
Check out the following link for more discussion:
http://*.com/questions/159978/c-sharp-keyword-usage-virtualoverride-vs-new