By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 974 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Overriding Methods and Virtual members in CS

P: n/a
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!
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"Rafael Veronezi" <li******@terra.com.br> wrote in message
news:eB**************@TK2MSFTNGP11.phx.gbl...
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!

Nov 15 '05 #2

P: n/a
Rafael Veronezi <li******@terra.com.br> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

P: n/a
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!

"Jon Skeet [C# MVP]" <sk***@pobox.com> escreveu na mensagem
news:MP***********************@msnews.microsoft.co m...
Rafael Veronezi <li******@terra.com.br> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4

P: n/a
Rafael Veronezi <li******@terra.com.br> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.