Click here to Skip to main content
15,909,737 members
Home / Discussions / C#
   

C#

 
AnswerRe: my Dictionary removal code correct? Pin
Guffa17-May-08 6:21
Guffa17-May-08 6:21 
GeneralRe: my Dictionary removal code correct? Pin
George_George17-May-08 21:12
George_George17-May-08 21:12 
GeneralRe: my Dictionary removal code correct? Pin
Guffa18-May-08 1:40
Guffa18-May-08 1:40 
GeneralRe: my Dictionary removal code correct? Pin
George_George18-May-08 1:43
George_George18-May-08 1:43 
AnswerRe: my Dictionary removal code correct? Pin
Roger Alsing17-May-08 7:22
Roger Alsing17-May-08 7:22 
GeneralRe: my Dictionary removal code correct? Pin
George_George17-May-08 21:07
George_George17-May-08 21:07 
Questionoverriding Pin
angels77717-May-08 3:09
angels77717-May-08 3:09 
AnswerRe: overriding [modified] Pin
Anthony Mushrow17-May-08 7:12
professionalAnthony Mushrow17-May-08 7:12 
OK
class A
{
  public virtual void Message()
  {
     Console.WriteLine("Original Message");
  }
}

class B : A
{
  public new void Message()
  {
     Console.WriteLine("Class B's Message");
  }
}

class C : A
{
  public override void Message()
  {
     Console.WriteLine("Class C's Message");
  }
}


Then, if you create an instance of each class and call Message() you will get each classes message. But, if you cast them as class A, the results are different:

A myA = new A();
B myB = new B();
C myC = new C();

myA.Message(); //Outputs "Original Message"
myB.Message(); //Outputs "Class B's Message"
myC.Message(); //Outputs "Class C's Messgae"

//Notice that these are of type A
A myAB = new B(); 
A myAC = new C();

myAB.Message(); //Outputs "Original Message"
myAC.Message(); //Outputs "Class C's Message"


When you use NEW it just hides the old method, so if you cast it as the base class it won't be able to see the new method anymore and the original will be called. If you use override, then its like re-writing the original method, so even though it is cast back as it's base class the derived classes method will still be called.

My current favourite word is: I'm starting to run out of fav. words!
-SK Genius

Game Programming articles start -here[^]-

modified on Saturday, May 17, 2008 1:21 PM

QuestionRunninng a clickonce deployed app from VBA Pin
rotsey17-May-08 3:08
rotsey17-May-08 3:08 
Questionwhy we need registry ? Pin
prasadbuddhika17-May-08 3:05
prasadbuddhika17-May-08 3:05 
AnswerRe: why we need registry ? Pin
PIEBALDconsult17-May-08 5:02
mvePIEBALDconsult17-May-08 5:02 
AnswerRe: why we need registry ? Pin
Hamid_RT17-May-08 7:35
Hamid_RT17-May-08 7:35 
Questionmemory footprint of Dictionary Pin
George_George17-May-08 1:50
George_George17-May-08 1:50 
AnswerRe: memory footprint of Dictionary Pin
Guffa17-May-08 3:26
Guffa17-May-08 3:26 
GeneralRe: memory footprint of Dictionary Pin
George_George17-May-08 3:35
George_George17-May-08 3:35 
GeneralRe: memory footprint of Dictionary Pin
Guffa17-May-08 6:29
Guffa17-May-08 6:29 
GeneralRe: memory footprint of Dictionary Pin
George_George17-May-08 21:31
George_George17-May-08 21:31 
GeneralRe: memory footprint of Dictionary Pin
Guffa18-May-08 12:34
Guffa18-May-08 12:34 
GeneralRe: memory footprint of Dictionary Pin
George_George20-May-08 22:40
George_George20-May-08 22:40 
GeneralRe: memory footprint of Dictionary Pin
Guffa20-May-08 23:17
Guffa20-May-08 23:17 
GeneralRe: memory footprint of Dictionary Pin
George_George20-May-08 23:43
George_George20-May-08 23:43 
GeneralRe: memory footprint of Dictionary Pin
Guffa21-May-08 3:28
Guffa21-May-08 3:28 
GeneralRe: memory footprint of Dictionary Pin
George_George21-May-08 4:28
George_George21-May-08 4:28 
GeneralRe: memory footprint of Dictionary Pin
Guffa21-May-08 11:17
Guffa21-May-08 11:17 
GeneralRe: memory footprint of Dictionary Pin
George_George25-May-08 1:38
George_George25-May-08 1:38 

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.