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 the
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 you
hide it, you still get the same results: the one on the derived class is
used instead.
The only difference I could find is the exception below, where new must be
used on MyBaseC instead of virtual and override b/c you cannot use virtual
with static.
So my question is- is there anything that new does that override doesn't or
vice-verse?
Aside from being usable with static? What's the difference between hiding
and overriding, or do they both just say "ignore the base, use the derived"?
This is by far the most difficult concept I've found in C#.
All examples look the same...
Again, any help *much* appreciated.
Thanks!
Jeff
public class MyBaseC
{
public static int x = 55;
public static int y = 22;
}
public class MyDerivedC : MyBaseC
{
new public static int x = 100; // Name hiding
public static void Main()
{
// Display the overlapping value of x:
Console.WriteLine(x);
// Access the hidden value of x:
Console.WriteLine(MyBaseC.x);
// Display the unhidden member y:
Console.WriteLine(y);
}
}
Output
100
55
22"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
z_learning_tester <so*****@microsoft.com> wrote: Any compare/contrast of using new vs using virtual and override much
appreciated.
See http://www.pobox.com/~skeet/csharp/faq/#override.new
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too