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]" <skeet@pobox.com> escreveu na mensagem
news:MPG.1a086ff1a66af3d989971@msnews.microsoft.co m...[color=blue]
> Rafael Veronezi <lixorafa@terra.com.br> wrote:[color=green]
> > I have some questions about override in inheritance, and virtual[/color][/color]
members.[color=blue][color=green]
> >
> > 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) {}[/color]
>
> That doesn't override it - that hides it.
>[color=green]
> > 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?[/color]
>
> 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.
>[color=green]
> > I wrote a struct where I override the Equals method, I did that first[/color][/color]
using[color=blue][color=green]
> > the override keyword, but the compiler throws a warning saying that I[/color][/color]
would[color=blue][color=green]
> > need to override the GetHashCode method too, why this? I would like to[/color][/color]
know[color=blue][color=green]
> > the diference about those declarations, and the real definition of[/color][/color]
virtual[color=blue][color=green]
> > methods![/color]
>
> 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 - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]