"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1b4323a236d8153698ad5e@msnews.microsoft.c om...[color=blue]
> z_learning_tester <someone@microsoft.com> wrote:[color=green]
> > OK, so I went throught the MSDN examples today and they say:
> >
> > Override is used in the derived class to say "use this one instead of[/color][/color]
the[color=blue][color=green]
> > one in the base class" I am overriding the one in the base class.
> > New also says "use this one instead of the one in the base class" I am
> > hiding the one in the base class.
> >
> > I still fail to see the difference if you override the base class or if[/color][/color]
you[color=blue][color=green]
> > hide it, you still get the same results: the one on the derived class is
> > used instead.[/color]
>
> The difference is what the *base* class will use if it calls the
> method. The link I provided gave an example of the difference, saying
> which would be called when.[/color]
Yep, that seems the part I was really missing, that seems the key!
These derived classes are not to be used on their own.
I think the WHOLE point is to put all these methods back into the base class
type by a cast, or into a base class type array, etc, then use that instance
of the base class type.
Their choice of verbage however, though I'm sure is very standard,
is also very unintuitive in regards to hiding because all the lingo seems
reversed.
That caught me up for so long...
After all, the base method that is "hidden"(by the derived class- using
"new") is the one that shows up!
The one that is "hidden" by the derived class using no kw at all, also shows
up.
Dang that's confusing.
Now override is the only one that seems to make sense,
if it overrides the base then it well, overrides it, and is used instead of
the base(great! :-)
// versioning.cs
// CS0114 expected
public class MyBase
{
public virtual string Meth1() //This one is "overridden" so it
doesnt show- OK :-)
{
return "MyBase-Meth1";
}
public virtual string Meth2() //This one is "hidden", so it shows!
{
return "MyBase-Meth2";
}
public virtual string Meth3() //This one is "hidden", so it shows!
{
return "MyBase-Meth3";
}
}
class MyDerived : MyBase
{ public override string Meth1() //This one "overrides" the base-
cool.
{
return "MyDerived-Meth1";
}
public new string Meth2() //Usually in life, the 'newer' thing
is used... {
return "MyDerived-Meth2" }
public string Meth3() //This one "hides" the base, so on
output, it is itself- hidden. {
return "MyDerived-Meth3";
}
public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;
System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}Output
MyDerived-Meth1
MyBase-Meth2
MyBase-Meth3[color=blue]
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]