How would you pass it? interfaces need an instance... use a singleton
pattern to get around this.

On 5/24/07, 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?

Anyone know?

Thanks,
Erik Erikson

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com



--
Studying for the Turing test

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to