Technical C# Q: why does the delegate need to be public?

On line 25, we have

public delegate void OnEvent();

I understand that the ‘void’ and the ‘()’ are used to sign the function delegate prototype. But I don’t understand why it needs to be public? Afterall, on the following line, 26, we have another public class member

public OnEvent onEventObservers;

to instantiate an observer set. Since this onEventObservers observer set is public, other classes can happily subscribe to it. So is there any good reason to leave OnEvent as public too?

I played around and googled a bit and learned that apparently C# demands that events are only as accessible as the delegate associated with it.

I suppose part of my confusion is why we have two lines at all: surely the compiler could read something like

public delegate RETURN_TYPE onEventObservers(INPUT_SIGNATURE_TUPLE);

and bypass the auxillary OnEvent thing all together. Is this not possible for some deep reason?

I believe - well at least Ive always been lead to believe it stems from : https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers

We create a delegate for the event, which is in effect a method we havent written yet, but we know its going to look like “<< this >>” as a result then for other people to be able to for-fill that they have to be able to see what it is. Otherwise the delegate type is only visible to the class it was in, and others couldnt then match it.

Privacy & Terms