Question

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.

Was it helpful?

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.

OTHER TIPS

Just in case someone else/new to C# needs more concrete explanation/examples:

public class ExampleClass
{
    // DelegateNames = NameOfObject + Verb + ("ed" or "ing") + "Handler".
    // NameOfObject  = Data, File, Property, Value, Window, Settings, User,...
    // Verb          = Open, Close, Format, Clear, Delete, Create, Insert, Update, Refresh, Dispose, Sort,...
    public delegate void SomethingHappenedHandler(object pSender, object pWhat);
    public delegate void SomeValueChangedHandler(object pSender, object pWhat, object pOldValue, object pNewValue);
    public delegate void SomethingFinishedHandler(object pSender);
    public delegate void SomethingInvokedHandler();

    // EventNames = DelegateName - "Handler".
    public event SomethingHappenedHandler SomethingHappened;
    public event SomeValueChangedHandler SomeValueChanged;
    public event SomethingFinishedHandler SomethingFinished;
    public event SomethingInvokedHandler SomethingInvoked;

    // EventHandlingMethodNames = "On" + EventName.
    private void OnSomethingHappened(object pSender, object pWhat) { /* Code...*/ }
    private void OnSomeValueChanged(object pSender, object pWhat, object pOldValue, object pNewValue) { /* Code...*/ }
    private void OnSomethingFinished(object pSender) { /* Code...*/ }
    private void OnSomethingInvoked() { /* Code...*/ }

    // EventInvokingMethods = ("Fire" | "Notify" | "Invoke") + EventName.
    private void FireSomethingHappened(object pSender, object pWhat) { /* SomethingHappened?.Invoke(...); */ }
    private void NotifySomeValueChanged(object pSender, object pWhat, object pOldValue, object pNewValue) { /* SomeValueChanged?.Invoke(...); */ }
    private void InvokeSomethingFinished(object pSender) { /* SomethingFinished?.Invoke(...); */ }
    private void NotifySomethingInvoked() { /* SomethingInvoked?.Invoke(...); */ }
}

For other program elements in C#, I use (you can) use following optional rules:

  1. Use always same character-casing (UpperCamelCase or lowerCamelCase) of your language!
  2. Always follow the naming-conventions/-rules of your programming language! (see/analyze/copy namings in frameworks!)
  3. I use prefixes for naming, if and only if the name is not violating against rule 0 and 1,
  • like: "p" + ParameterName (for better recognition between local variables and parameters
  • or: "m" + MemberVariableName (for better recognition between local and member variables.
  • or: "I" + InterfaceName (like: IDeserialize, IXmlConvert,...)
  • or: "A" + AbstracClassName (like: ADeserializer, AXmlConvert,...)
  • or: "E" + EnumerationName (like: EState, EErrorCode, EMemberType,...)
  • or: "S" + StaticVarialeNames
  • and: ALL_UPPER_CASE for:
    • compiletime constants "public const double MY_CONST = -1.23;"
    • runtime constants: "public static readonly MyClass MY_CLASS = new MyClass()...")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top