473,414 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

abstract new ???

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
Nov 16 '05 #1
6 1338
I suppose virtual new means override

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

"Chris" <ch********@pandora.be> wrote in message
news:YN*********************@phobos.telenet-ops.be...
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

Nov 16 '05 #2
use Roeder's .NET Reflector http://www.aisto.com/roeder/dotnet/

for better and more accurate object browsing, it even decompiles the code on
the fly.
"Chris" <ch********@pandora.be> wrote in message
news:YN*********************@phobos.telenet-ops.be...
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

Nov 16 '05 #3
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.


Nov 16 '05 #4
mdb
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
news:Od**************@TK2MSFTNGP15.phx.gbl:
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
Nov 16 '05 #5
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

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
news:Od**************@TK2MSFTNGP15.phx.gbl:
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

Nov 16 '05 #6
Sorry, I mean it will print out the type name even if it overrides ToString itself

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
ugghhhh. now when any derived class object is passed to Console.WriteLine it will print out a blank - isn;t that an obvious behavior.

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Merlin | last post by:
Hi Been reading the GOF book and started to make the distinction between Class and Interface inheritance. One question though: Do pure abstract classes have representations? (data members?)...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
5
by: Tony Johansson | last post by:
Hello!! Assume you have an Interface called ITest with these three method declarations. interface ITest { void foo1(); void foo2(); void foo3(); }
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.