[Mono-list] Base class casting weirdness
Daniel Lewis
dihlewis@yahoo.co.uk
Thu, 17 Jan 2002 01:13:11 +0000 (GMT)
Hi,
I wonder if anyone can shed light on this. In C#, the 'base' keyword is
used to access methods and constructors of base classes:
class A {
public virtual string Foo () { return "A"; }
}
class B : A {
public override string Foo () { return base.Foo (); }
}
Invoking Foo on an instance of B returns "A", as you'd expect. The C#
programmer's reference that is packaged with the SDK states that:
base.Foo ()
.. called in an instance method of B is exactly equivalent to writing:
((A)this).Foo ()
And that in fact this is what the compiler replaces it with at compile
time. But if you try to implement it this way:
public override string Foo () { return ((A)this).Foo (); }
CSC will compile it, but you'll get a StackOverflowException at runtime.
Why would I want to write it this way instead of using 'base'? Well, I've
got a class C derived from B, that wants to implement Foo by invoking A's
definition. Can anyone think of a way to do this? Is the runtime
exception correct, or is this an SDK bug? And what does MCS/Mono do with
this?
Thanks,
Dan.
__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com