Delegate Volume in C #

Can delegates be private? If not, what is the reason for this, besides the usual restrictions caused by the fact that it is private?

+6
scope c # delegates
source share
1 answer

Delegates have the same restrictions as any type with respect to visibility. Therefore, you cannot have a private delegate at the top level .

namespace Test { private delegate void Impossible(); } 

This creates a compiler error:

Elements defined in the namespace cannot be explicitly declared as private, protected, or protected internal

But, as a class, you can declare a delegate private when it is in another class .

 namespace Test { class Sample { // This works just fine. private delegate void MyMethod(); // ... } } 

The reason basically goes back to determining that private is in C #:

private | Access is limited to the containing type.

+14
source share

All Articles