views:

185

answers:

1

Hi folks, can delegates be private, if not what's the reason behind this other than the normal restrictions caused by it being private.

TIA

+8  A: 

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

namespace Test
{
    private delegate void Impossible();
}

This generates a compiler error:

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

But like a class, you can declare a delegate private when it resides within another class.

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

        // ...
    }
}

The reason basically goes back to the definition of what private is in C#:

private | Access is limited to the containing type.

bobbymcr
+1: beat me, and more complete answer!
Mark Byers
nice, thanks bobbymcr. sometimes, we need a reinforcement of concepts/basics.
SoftwareGeek