Correct naming convention for type .NET Delegate?

By conditional classes are often called names, such as verbs and interfaces, like adjectives.

What is the general naming convention for a delegate? Or what good way to distinguish his name when delegates are listed among types and other things?

My immediate assumption is rather to call the delegate rather adjective, because one method interface can often be replaced by a delegate.

Some thoughts:

delegate object ValueExtracting(object container); delegate object ValueExtractor(object container); delegate object ValueExtractionHandling(object container); delegate object ValueExtractionHandler(object container); 
+74
naming-conventions delegates
Feb 27 '10 at 3:00
source share
7 answers

Personally, I use a couple of different templates:

[Task][State]Handler - UITaskFinishedHandler

[Event]Handler - ControlLoadedHandler

[Function Name]Delegate - DoSomeWorkDelegate - used when I need to create a delegate to call a function in another / new thread

[Task]Callback - ContainerLoadedCallback - is used when the control A starts an action that B controls most of the work for, and the control A has passed a dependency for control B (that is, ControlA could go through the container interface for ControlB to fill and requires a notification for the actual container display)

When you have a project that uses many multi-threaded or asynchronous WCF calls, you may encounter a lot of delegates floating around, so it’s important to adopt a standard that at least makes sense to you.

+98
Feb 27
source share

The Microsoft Framework Design Guidelines - a personalized almanac for me, says the following on the topic:

√ Add the suffix "EventHandler" to the delegate names used in events.
√ Add the callback suffix to the names of delegates, except for those used as event handlers.
X DO NOT add the delegate suffix to the delegate.

+37
Sep 23 '14 at 11:23
source share

Since the delegate is what performs the action (verb), the delegate should be called what you would call what performs this action. Take Converter<TInput, TOutput> , for example. Verb Convert . What the conversion does is called a converter , therefore the name of the delegate.

+15
Feb 27 '10 at 4:07
source share

It depends on a few things.

If a delegate will be used as an event, it should always be called a subtype of EventHandler , for example:

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

If this is not an event, then the MS coding guide (which I can never find the right copy on Google) explicitly recommend not to include words such as "delegate" or "handler" in the delegate name, except in the special case of EventHandler types.

Typically, delegates should be named after actions that will be similar to ValueExtracting (if delegation occurs before the value is retrieved) or ValueExtracted (after retrieval).

The delegate syntax Func<T1, T2, ..., TResult> also becoming more common, but if you do not have 4 or more parameters that you need, you do not need to declare your rights at all - just use the existing one:

 object ExtractObject(object source, Func<object, object> extractor); 

This syntax is best when a delegate is used as a closure. The delegate itself does not have a very interesting name, but the argument is a noun of the agent (extractor, supplier, evaluator, selector, etc.).

Most delegate applications fit into one of the above categories, so find out which one it uses to select accordingly.

+6
Feb 27
source share

I never thought about this, mainly because I just use one of the EventHandler<T> , Func<T> or Action<T> overloads and will never define my own. I would probably choose a ValueExtractor from the ones you specified. This makes the sound more like an object, and when you call it, you will use this object to perform the action. For example:

 ValueExtractor extractor += Blah; var value = extractor(data); 

In addition, most built-in delegates are called names. If in doubt, follow the .NET platform.

+3
Feb 27 2018-10-02T00
source share

I would go with ValueExtraction ..
I never thought why, but I think because you are storing the operation, and it must be a noun. strictly this is not an operation, I know ...

0
Feb 27 '10 at 3:14
source share

Based on Enumerable.Sum I passed the delegate as Func<object, object> and I will name the selector parameter:

 void Foo(Func<object, object> selector) ... 

If you need to make your own delegate for this, I would go with a ValueExtractor , as this is the most descriptive name for creating it.

0
Feb 27 '10 at 3:19
source share



All Articles