Yes, it's a bit cumbersome, the relationship between static and interface. Interfaces are intended to define an object contract for polymorphism. You can't pass an object around to be cast to different interface references if you can't instantiate the class. So, statics and interfaces are not supported.
Yes, one of the side-effects of deriving from an interface is that you're mandated (i.e. it's a compile error if you don't) that you implement the members declared in the interface. That's not the main intention of interfaces and, as such, not a reason to support static classes deriving from interfaces (other than the forced implementations you can't do anything with it). On Thu, 24 May 2007 19:30:20 -0700, Erik M. Erikson <[EMAIL PROTECTED]> wrote: >I've got a question, hoping that maybe someone out there has a better >answer than my local theory-wonk. :) > >Qualification... I don't believe that this is necessarily best (or even >good) practice (or anything of the like), but it brings up what is to me >an interesting question and I'm hoping someone has an idea. > >The question: Why was it decided that static classes cannot implement >interfaces? > >To be concrete, the following won't compile: > > 1 namespace StaticClassWithInterface { > 2 public static class StaticClass : IType2 { > 3 private static readonly object _Lock = new object(); > 4 private static Type1 _Type1 = new Type1(); > 5 private static readonly Type2 _Type2 = new Type2(); > 6 > 7 public static Type1 Type1Obj { > 8 get { > 9 lock ( _Lock ) { >10 return _Type1; >11 } >12 } >13 } >14 >15 public static void Update( object o ) { >16 lock ( _Lock ) { >17 _Type1.Update(); >18 _Type2.Update( o ); >19 } >20 } >21 } >22 public class Type1 { >23 public void Update() { } >24 } >25 public class Type2 : IType2 { >26 public void Update( object o ) { } >27 } >28 public interface IType2 { >29 void Update( object o ); >30 } >31 } > >On a side-note, the fun part is that when you click on the following >error in the "Error List" tab in Visual Studio, it takes you to the >interface definition (with IType2 underlined in blue): > >Error 1 'StaticClassWithInterface.IMyType': static classes >cannot implement interfaces ~\Class1.cs 25 22 >StaticClassWithInterface > >Incidentally, the ouput tab directs you to the related code if you >didn't figure out where the violation was for yourself: > >~\Class1.cs(25,22): error CS0714: 'StaticClassWithInterface.IMyType': >static classes cannot implement interfaces >~\Class1.cs(2,25): (Related location) > >Now... I have completely abandoned the approach I was taking (which is >only a temporary measure) but I am left wondering why have static >classes been restricted from implementing interfaces? =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com