Search code examples
c#eventsconventions

Categorizing C# Events, Delegates, Callbacks, etc


Simple question, I think: How do you categorize/name events-related items in C#?

For example, my current conventions look something like the following:

public class A
{
    ...
    #region Delegates
    public delegate void CallIncomingEventHandler(string displayName);
    #endregion


    #region Event Handlers
    public event CallIncoming;
    #endregion
    ...
}

public class B
{
    ...
    #region Events
    private void A_CallIncoming(string displayName)
    {
        // do stuff here
    }
    #endregion
}

My confusion is with defining "SomethingEventHandler" types with the "delegate" modifier and calling them "Delegates", then defining field-like events with the "event" modifier and calling them "Event Handlers", and then to call the methods that react to the events "Events".

Is my naming correct?

I can create and use custom events, etc., but I could use some help with understanding the names of the components and how others organize them.

Thanks.


Solution

  • You described conventions that are used within .NET aren't you?

    Personally I see no problem in your approach. It is well known and every developer who worked with .NET will most probably expect if not the same convention then close to it. So why should you invent something new when there is an adopted convention exists?

    Instead of this I would suggest to think about how events would be invoked within your classes and how would you for example extend parameters for delegates in future? A good starting point here would be again to follow .NET convention with delegates like delegate void SomethingEventHandler(object sender, EventArgs e). A good thoughts on this were given in this question.

    Hope this reduced your doubts.