[Mono-list] mcs won't allow implementation of interfaces with static methods

Serge serge@wildwestsoftware.com
Thu, 15 Aug 2002 04:06:19 +0300


> static methods are not allowed on interfaces

This is not allowed in C# but actually allowed in metadata.
See Part II, 11 Semantics of Interfaces
"Interfaces may have static fields and methods, but they shall not have instance fields or methods."

However, I believe, CSC issues error message as well in this case (when trying to implement interface with static methods).
So perhaps MCS behaviour is correct as well.

However, it seems that CSC has another error in this respect (I'm not 100% sure at the moment).

Consider the following ILASM code that has static member in interface.
(This could be achieved with MC++ as well, I believe).

.assembly StaticIfc {}

.class interface public auto ansi StaticIfc {
     .method public static int32 GetInt() cil managed {
          ldc.i4 0xACDC
          ret
     }
}

.class public auto ansi StaticImpl {
     .method public static int32 GetInt() cil managed {
          call int32 StaticIfc::GetInt()
          ret
     }
}


Now the following will compile correctly with CSC:

 public static void Main() {
    Console.WriteLine("0x{0:X}", StaticImpl.GetInt());
 }

While this will produce invalid code:

    Console.WriteLine("0x{0:X}", StaticIfc.GetInt());

It seems that MCS produces correct code in both cases.


Sergey


----- Original Message ----- 
From: "Martin Baulig" <martin@gnome.org>
To: "Jeroen Frijters" <mono@jeroen.nu>
Cc: <mono-list@ximian.com>
Sent: Thursday, August 15, 2002 2:36 AM
Subject: Re: [Mono-list] mcs won't allow implementation of interfaces with static methods


> "Jeroen Frijters" <mono@jeroen.nu> writes:
> 
> > When I have a C# class that tries to implement an interface (created
> > with ILASM) that contains a static method, mcs gives an error saying
> > that I should implement the static method (which obviously doesn't make
> > sense).
> 
> Hi,
> 
> I don't understand what you're trying to do: static methods are not
> allowed on interfaces, that'd be an error CS0106.  MCS currently
> doesn't report that error but abort with a parsing error instead since
> its parser won't recognize `static' as a keyword if it's used on an
> interface method.
> 
> ====
> using System;
> 
> interface A
> {
> static void Test ();
> }
> 
> class X : A
> {
> public static void Test ()
> {
> }
> 
> static void Main ()
> {
> }
> }
> ====
> 
> -- 
> Martin Baulig
> martin@gnome.org
>