Proper naming convention for a .NET Delegate type?

后端 未结 8 1997
我寻月下人不归
我寻月下人不归 2020-12-12 11:29

By convention classes are often named like nouns, methods like verbs and interfaces like adjectives.

What is the common naming convention for a delegate? Or what\'s

8条回答
  •  不思量自难忘°
    2020-12-12 11:51

    This depends on a few things.

    If the delegate is going to be used as an event, it should always be referred to as an EventHandler subtype, for example:

    public delegate void ValueExtractingEventHandler(object sender,
        ValueExtractingEventArgs e);
    

    If it's not an event, then the MS coding guidelines (which I can never seem to find the right copy of on Google) explicitly recommend against including words like "delegate" or "handler" in the delegate name, except in the special case of EventHandler types.

    Normally, delegates should be named after actions, which would be like ValueExtracting (if the delegate happens before the value is extracted) or ValueExtracted (after extraction).

    The Func delegate syntax is also becoming more common, but unless you have 4 or more parameters going into it, you don't need to declare your own at all - just use an existing one:

    object ExtractObject(object source, Func extractor);
    

    This syntax is best when the delegate is being used as a closure. The delegate itself doesn't have a very interesting name, but the argument is an agent noun (extractor, provider, evaluator, selector, etc.)

    Most delegate usages fit into one of the above categories, so figure out which one it's being used for choose appropriately.

提交回复
热议问题