Overriding Methods and Virtual members in CS

R

Rafael Veronezi

I have some questions about override in inheritance, and virtual members.

I know that you can you override a method by two ways in C#, one, is
overriding with the new keyword, like:

public new bool Equals(object obj) {}

Another is using the override keyword, like:

public override bool Equals(object obj) {}

And my question is, what's the main diference of using one or another? Also,
I know about a keyword called virtual. I thought that the virtual keyword is
used to identify a method that can be overriden... I'm not sure about that,
I would like to know a better explain of virtual methods, cause I think that
using the new keyword, you can override any method in the inhereted class
(not sure about that)...

I wrote a struct where I override the Equals method, I did that first using
the override keyword, but the compiler throws a warning saying that I would
need to override the GetHashCode method too, why this? I would like to know
the diference about those declarations, and the real definition of virtual
methods!

Thanks guys!
 
N

news.microsoft.com

new bool Equals is only virtual from that point, wherase an overrise of an
exist virtual is virtual all the way up to the virtual definition
 
J

Jon Skeet [C# MVP]

Rafael Veronezi said:
I have some questions about override in inheritance, and virtual members.

I know that you can you override a method by two ways in C#, one, is
overriding with the new keyword, like:

public new bool Equals(object obj) {}

That doesn't override it - that hides it.
Another is using the override keyword, like:

public override bool Equals(object obj) {}

And my question is, what's the main diference of using one or another?

When you really override a method, it acts polymorphically. When you
hide it, it's like a new method. Here's an example to explain it:

using System;

class Base
{
public virtual void Foo()
{
Console.WriteLine ("Base.Foo");
}
}

class OverridesFoo : Base
{
public override void Foo()
{
Console.WriteLine ("OverridesFoo.Foo");
}
}

class HidesFoo : Base
{
public new void Foo()
{
Console.WriteLine ("HidesFoo.Foo");
}
}

class Test
{
static void Main()
{
Base x = new Base();
x.Foo();
x = new OverridesFoo();
x.Foo();
x = new HidesFoo();
x.Foo();

OverridesFoo y = new OverridesFoo();
y.Foo();

HidesFoo z = new HidesFoo();
z.Foo();
}
}

The output is:
Base.Foo
OverridesFoo.Foo
Base.Foo
OverridesFoo.Foo
HidesFoo.Foo

The first line is Base.Foo because the object in question just *is* a
Base instance.

The second line is OverridesFoo.Foo because the object in question is
an OverridesFoo instance, and it overrides the Base.Foo method.

The third line is Base.Foo because although the object in question is a
HidesFoo instance, the HidesFoo.Foo method doesn't override the
Base.Foo method - it's essentially a separate method which happens to
have the same name. The compiler only knows of the reference as being
of type Base.

The fourth line is OverridesFoo.Foo for hopefully obvious reasons.

The fifth line is HidesFoo.Foo because this time the compiler knows
that the reference is a HidesFoo, so it calls HidesFoo.Foo rather than
BaseFoo.Foo.
I wrote a struct where I override the Equals method, I did that first using
the override keyword, but the compiler throws a warning saying that I would
need to override the GetHashCode method too, why this? I would like to know
the diference about those declarations, and the real definition of virtual
methods!

When you override Equals you should also override GetHashCode so that
two equal objects return the same hash code - otherwise if you try to
use an instance of your struct as the key for a hashtable, it may not
work properly.
 
R

Rafael Veronezi

I think that I understood the difference between using new and override...
But I even don't know when I would use one or another?
Another question I was made was about the virtual keyword, I can only
override virtual methods?
Thanks!
 
J

Jon Skeet [C# MVP]

Rafael Veronezi said:
I think that I understood the difference between using new and override...
But I even don't know when I would use one or another?

You'd almost *always* use override - hiding methods is almost always a
bad thing, because it's sometimes hard to follow what's going on.
Another question I was made was about the virtual keyword, I can only
override virtual methods?

Yes (well, those and abstract methods). The whole point of making a
method virtual is so that it can be overridden.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top