Reputation: 12498
This gives me a compiler error:
public static delegate void SomeCoolDelegate();
Why?
Upvotes: 3
Views: 1355
Reputation: 12498
Ok so I think what happens is this:
Declaring a delegate is really asking the compiler to create a new special class for you. The job of this class is to act as a function pointer (or multiple function pointers if needed).
The compiler will oblige by creating a new class as you direct with all the things you need to get your job done with the delegate. It will go ahead and create this class, but all the things you are getting are instance level methods and properties, etc.
So declaring a delegate as static is nonsensical (As JaredPar and others pointed out). You are saying 'give me a static class wherein all the guts are not static (and thus unreachable), etc. etc." It doesn't make sense.
Upvotes: 1
Reputation: 39916
Delegate is a type, in runtime perspective type is just classification of data store like a folder containing members, there is no point of making a type static because type is static anyway. Member of any type can be static.
C# let's you write static on class to restrict developer to write only static members in type but that does not mean that type is or isn't static.
Upvotes: 0
Reputation: 41236
That has no meaning for a delegate; I mean, semantically, it's like saying I never want an implementation of this method signature!
You could declare an implementation as static...
protected static SomeCoolDelegate SomeMethod { get; set; }
When you mark a class as static, you're saying you never want it instantiated. A delegate is simply a way of identifying a method signature.
Upvotes: 6
Reputation: 754715
Declaring a delegate is declaring a type. It makes very little since for a type which contains only instance methods to be declared static.
Upvotes: 2
Reputation: 86718
Declaring a delegate
is declaring a type. Delegates in C# cannot be made static
. If you want to declare a variable of type delegate, you would do something like this:
public delegate void SomeCoolDelegate(); // declare a type called SomeCoolDelegate
public static SomeCoolDelegate foo; // declare foo as an object of type SomeCoolDelegate
Upvotes: 4