Click here to Skip to main content
15,889,879 members
Home / Discussions / C#
   

C#

 
GeneralRe: Counting set bits Pin
Philip Fitzsimons23-Feb-05 6:59
Philip Fitzsimons23-Feb-05 6:59 
GeneralRe: Counting set bits Pin
PaleyX23-Feb-05 7:10
PaleyX23-Feb-05 7:10 
GeneralRe: Counting set bits Pin
Philip Fitzsimons23-Feb-05 7:15
Philip Fitzsimons23-Feb-05 7:15 
GeneralRe: Counting set bits Pin
PaleyX23-Feb-05 7:26
PaleyX23-Feb-05 7:26 
GeneralRe: Counting set bits Pin
Sebastian Schneider25-Feb-05 2:34
Sebastian Schneider25-Feb-05 2:34 
GeneralRe: Counting set bits Pin
PaleyX25-Feb-05 2:52
PaleyX25-Feb-05 2:52 
Generalfunctions with override and new keywords .... Pin
vchedalla23-Feb-05 5:10
vchedalla23-Feb-05 5:10 
GeneralRe: functions with override and new keywords .... Pin
turbochimp24-Feb-05 13:17
turbochimp24-Feb-05 13:17 
To begin with, both these topics are discussed in the online help, as well as probably hundreds of indy websites around the globe, but I'll try to give a brief reply that might help clarify things:

1. A subclass method marked with "override" indicates that the programmer has a subclass in which he does not desire to (only) use the base class implementation of a particular method. The developer may still choose to execute the base implementation (e.g. base.SomeVirtualMethod() ) but he can choose to completely replace - or "override" - the base implementation if desired.

2. A subclass method marked with "new", or one that bears the same name as a base method which has no "override" modifier on it, "hides" (C#) or "shadows" (VB) the base method. Subclass methods that hide base methods can (like overrides) also completely replace the functionality of the base implementation.

The main difference between "override" and "new", and certainly the one that underscores the danger in using "new" indescriminately, is in regards to execution of the virtual methods.

If you have overridden base methods, it does not matter how your variables are declared - it's the way they are instantiated that matters, so even if an instance is "upcasted" to the base type, the overridden implementation will be called. This is very important - it's part of what makes concepts like polymorphism work.

However, using "new" has some potentially unexpected results.
Assuming we are using your example above, and we write:
MyDerived md = new MyDerived();
string foo = md.Meth2();

We would get the expected result; the derived class' implementation of Meth2, or "MyDerived-Meth2".

However, if we wrote:
MyBase mb = new MyDerived();
string foo = mb.Meth2();

We would actually get the base class implementation of Meth2's return, or "MyBase-Meth2".

Careless or uninformed use of hiding can have very serious implications and confusing results. In general, override should "always" be used. I put "always" in quotes because it's impossible to say "always" and get away with it. There are some possible cases where hiding methods is acceptable. For instance if you had a third party assembly to which you had no source, and one of its types had a method that was non-virtual, and did something that made your life impossible, then *maybe* hiding the base implementation would be okay.

I can't think of a case for hiding a virtual base method, though, except human error.

I hope this helps.


The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’

GeneralNewline in a TextBox Pin
pjholliday23-Feb-05 4:41
pjholliday23-Feb-05 4:41 
GeneralRe: Newline in a TextBox Pin
Sebastian Schneider23-Feb-05 4:58
Sebastian Schneider23-Feb-05 4:58 
GeneralRe: Newline in a TextBox Pin
Dave Kreskowiak23-Feb-05 5:29
mveDave Kreskowiak23-Feb-05 5:29 
GeneralRelease an object memory Pin
Sasuko23-Feb-05 4:27
Sasuko23-Feb-05 4:27 
GeneralRe: Release an object memory Pin
PaleyX23-Feb-05 5:09
PaleyX23-Feb-05 5:09 
GeneralRe: Release an object memory Pin
Dave Kreskowiak23-Feb-05 5:27
mveDave Kreskowiak23-Feb-05 5:27 
Generaldisplay a pic from the database Pin
Ahmed Galal23-Feb-05 4:05
Ahmed Galal23-Feb-05 4:05 
GeneralRe: display a pic from the database Pin
Sasuko23-Feb-05 4:31
Sasuko23-Feb-05 4:31 
GeneralRe: display a pic from the database Pin
Ahmed Galal23-Feb-05 6:54
Ahmed Galal23-Feb-05 6:54 
GeneralRe: display a pic from the database Pin
Ahmed Galal23-Feb-05 7:24
Ahmed Galal23-Feb-05 7:24 
GeneralSharing a Udp Port Pin
Zishan Haider23-Feb-05 4:02
Zishan Haider23-Feb-05 4:02 
GeneralRe: Sharing a Udp Port [EDITED] Pin
Dave Kreskowiak23-Feb-05 5:06
mveDave Kreskowiak23-Feb-05 5:06 
Generalcombobox and SelectedIndexChanged in 2.0 Pin
hartree23-Feb-05 1:46
hartree23-Feb-05 1:46 
GeneralPlease Helllllllllllllppppppp me :sigh: Pin
ahmed Za3roor23-Feb-05 1:35
ahmed Za3roor23-Feb-05 1:35 
GeneralRe: Please Helllllllllllllppppppp me :sigh: Pin
J4amieC23-Feb-05 2:04
J4amieC23-Feb-05 2:04 
Generalhtml or xml control/editbox (urgent) Pin
saima azeemi22-Feb-05 23:28
saima azeemi22-Feb-05 23:28 
GeneralRe: html or xml control/editbox (urgent) Pin
Rei Miyasaka23-Feb-05 0:40
Rei Miyasaka23-Feb-05 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.