Hi Marcel,
1.) Why do I have to use the override and virtual keys words, if it runs
without ? Is it for better readability ?
2.) When do I've to use virtual and override and when new ? I mean is
there a rule which describes in this case it should be used virtual and
override and in some other cases better use new ?
When using inheritance if you have a base class which has a method called
X() and a derived class (a class that inherits from the base class) also has
a method called X() then the derived classes method X just hides the
implementation of the base class, but it does not override the implementation
of the base class. Meaning that if you refer to the derived class D with a
reference of type D you will see the code you implemented in D, however if
you refer to an instance of D with a reference of the base type B, then you
will get the code that runs in B, for example:
using System;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
// "Print Person" is the output
Person p = new Person();
p.Print();
// "Print Man" is the output
Man m = new Man();
m.Print();
// "Print Person" is the output
Person manPerson = new Man();
manPerson.Print();
Console.ReadLine();
}
}
class Person
{
public void Print()
{
Console.WriteLine("Print Person");
}
}
class Man : Person
{
public void Print()
{
Console.WriteLine("Print Man");
}
}
}
The "new" keyword in this context is used to indicate that you realize that
there is a method in the base class you are hiding and you accept it. By
placing the "new" keyword on your derived classes method you are saying that
you realize there is method hiding taking place and you are okay with it.
This will stop the compiler warning you are getting. This is good because
you don't want this to take place silently behind your back because this
could cause significant bugs. In the code above the compiler would give you
the warning:
Warning 1 'ConsoleApplication21.Man.Print()' hides inherited member
'ConsoleApplication21.Person.Print()'. Use the new keyword if hiding was
intended. C:\test\ConsoleApplication21\Program.cs 35 21 ConsoleApplication21
Now if you don't want to just let people who inherit from a base class to
hide a method but completely replace the implementation you can mark the
method as virtual. People who want to replace the implementation then just
create a function with the same signature in the derived class and use the
override keyword. If we go back to the code example above and use the
virtual and override keyword you will see that now the output changes from:
Print Person
Print Man
Print Person
to
Print Person
Print Man
Print Man
using System;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
// "Print Person" is the output
Person p = new Person();
p.Print();
// "Print Man" is the output
Man m = new Man();
m.Print();
// "Print Man" is now the output
Person manPerson = new Man();
manPerson.Print();
Console.ReadLine();
}
}
class Person
{
public virtual void Print()
{
Console.WriteLine("Print Person");
}
}
class Man : Person
{
public override void Print()
{
Console.WriteLine("Print Man");
}
}
}
Finally, you can still access the base classes implementation even if you
override it by using the base keyword, so in the above example from
Man:
rint you can call Person:
rint by saying base.Print(). This not only
allows you to replace the base implementation but also to extend it.
Now with great power come great responsibility, when using these methods it
is easy to break things and cause bugs, you should be aware of what you are
doing and what the base class is doing so that you do not have problems.
I hope that clears things up a bit for you.
Mark Dawson
http://www.markdawson.org