D
Darius
I'm writing here in hopes that someone can explain the difference
between the new and virtual/override keywords in C#.
Specifically, what is the difference between this:
public class Window
{
public void Draw()
{
Console.WriteLine("The WINDOW Draw method is
running!");
}
}
class Listbox : Window // Listbox inherits from Window
{
public new void Draw()
{
Console.WriteLine("The LISTBOX Draw method is
running!");
}
}
And this ...
public class Window
{
public virtual void Draw()
{
Console.WriteLine("The WINDOW Draw method is
running!");
}
}
class Listbox : Window // Listbox inherits from Window
{
public override void Draw()
{
Console.WriteLine("The LISTBOX Draw method is
running!");
}
}
I've actually done some searching on this topic and from what I've
read, if you use the new keyword in the derived class, then it 'hides'
the method in the base class. Ok, hides it from WHAT?? Others have
said that if you use the new keyword, then it doesn't 'act
polymorphically' ... whatever that means.
From what I can tell, the real difference comes when you start
instantiating the objects. Like normally, I guess you would do this to
get a reference to the Listbox class from above:
Listbox l = new Listbox();
But apparently, if you've been smoking the fatty quite hard, you might
do this instead:
Window l = new Listbox();
So, why the hell would you instantiate a Listbox object from a Window
object? Or is it a Window object from a Listbox object? I mean, what
are the 'rules' when you do something like that?
Um, so ... can anyone help ?
between the new and virtual/override keywords in C#.
Specifically, what is the difference between this:
public class Window
{
public void Draw()
{
Console.WriteLine("The WINDOW Draw method is
running!");
}
}
class Listbox : Window // Listbox inherits from Window
{
public new void Draw()
{
Console.WriteLine("The LISTBOX Draw method is
running!");
}
}
And this ...
public class Window
{
public virtual void Draw()
{
Console.WriteLine("The WINDOW Draw method is
running!");
}
}
class Listbox : Window // Listbox inherits from Window
{
public override void Draw()
{
Console.WriteLine("The LISTBOX Draw method is
running!");
}
}
I've actually done some searching on this topic and from what I've
read, if you use the new keyword in the derived class, then it 'hides'
the method in the base class. Ok, hides it from WHAT?? Others have
said that if you use the new keyword, then it doesn't 'act
polymorphically' ... whatever that means.
From what I can tell, the real difference comes when you start
instantiating the objects. Like normally, I guess you would do this to
get a reference to the Listbox class from above:
Listbox l = new Listbox();
But apparently, if you've been smoking the fatty quite hard, you might
do this instead:
Window l = new Listbox();
So, why the hell would you instantiate a Listbox object from a Window
object? Or is it a Window object from a Listbox object? I mean, what
are the 'rules' when you do something like that?
Um, so ... can anyone help ?
