abstract new ???

C

Chris

hi,

looking at what the object-browser is displaying in VisStudio.NET for
classes and interfaces, I noticed weird keyword-combinations for quite a lot
definitions :

to mention a few :

1) Object class
public virtual new System.Boolean Equals ( System.Object obj )
public virtual new System.String ToString ( )

virtual new ??? shouldn't it just be virtual ?

2) public abstract interface IDisposable
public abstract new void Dispose ( )

abstract interface??? shouldn't it just be interface ?
abstract new ??? shouldn't it just be abstract ?

what's going on here ?
thnx
Christian
 
B

Bern

I suppose virtual new means override

although abtract interface is invalid in C#, but it is valid in the MSIL.
 
R

Richard Blewett [DevelopMentor]

new virtual is the spawn of Satan.

class A
{
public virtual void Foo()
{
}
}

class B : A
{
public override void Foo()
{
}
}

class C : B
{
public new virtual void Foo()
{
}
}

class D : C
{
public override void Foo()
{
}
}

class App
{
static void Main()
{
A a = new D();
a.Foo(); // calls B.Foo

C c = (C)a;
c.Foo(); // calls D.Foo
}
}

Unfortunately its a spawn of Satan that is necessary in situations where base and derived classes evolve independently and a new version of the base class ends up with a method with the same name and signature as an existing virtual method in the derived class.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I suppose virtual new means override

although abtract interface is invalid in C#, but it is valid in the MSIL.
 
M

mdb

new virtual is the spawn of Satan.

Your example is nice... however I think you are making it sound like a
difficult concept to grasp.

The 'new' keyword simply identifies the point at which overridden functions
become "invisible" when called from objects declared as a base type of the
class in question. The 'virtual' keyword indicates that a function might
be overridden in derived classes and that objects should try to use the
most overridden function possible.

There's nothing conflicting between these two concepts, as long as one
understands that 'invisible' is not 'possible'.

-mdb
 
R

Richard Blewett [DevelopMentor]

For people new to OO and polymorphism it is difficult to grasp (believe me I teach alot of people C# from non-OO backgrounds). The problem with new virtual is you have to know the details of every class in derivation chain to understand what is going to happen when you call a method through the base class - this to me is bad. If I override a method from the bottom of my derivation chain and then find a different version is being called - where do I start to look? Once you know about the evils of new virtual you may look in the inheritance chain and see someone has used new virtual.

How about if I write a class that does this:

class Foo
{
public new virtual string ToString()
{
return string.Empty;
}
}

ugghhhh. now when any derived class object is passed to Console.WriteLine it will print out a blank - isn;t that an obvious behavior.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

new virtual is the spawn of Satan.

Your example is nice... however I think you are making it sound like a
difficult concept to grasp.

The 'new' keyword simply identifies the point at which overridden functions
become "invisible" when called from objects declared as a base type of the
class in question. The 'virtual' keyword indicates that a function might
be overridden in derived classes and that objects should try to use the
most overridden function possible.

There's nothing conflicting between these two concepts, as long as one
understands that 'invisible' is not 'possible'.

-mdb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top