[Mono-list] Base class casting weirdness

Rogier Brussee brussee@telin.nl
Fri, 18 Jan 2002 14:55:16 +0100


Hi,
 
I don't really know C#, but extrapolating the solution for C++: if you
want to explicitly use the foo method from class A, try qualifying the
method explcitly i.e. try

this . A.foo() 

Rogier
 

On Thu, Jan 17, 2002 at 01:13:11AM +0000, Daniel Lewis wrote:
> 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?