.NET 委托类型的正确命名约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2346065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper naming convention for a .NET Delegate type?
提问by John K
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 a good way to differentiate its name when delegates are listed among types and other things?
委托的通用命名约定是什么?或者当代表在类型和其他事物中列出时区分其名称的好方法是什么?
My immediate assumption is to name a delegate more likely an adjectivebecause a single method interface can often be replaced with a delegate.
我的直接假设是将委托命名为更可能是形容词,因为单个方法接口通常可以替换为委托。
Some thoughts:
一些想法:
delegate object ValueExtracting(object container);
delegate object ValueExtractor(object container);
delegate object ValueExtractionHandling(object container);
delegate object ValueExtractionHandler(object container);
回答by slugster
Personally I use a couple of different patterns:
我个人使用几种不同的模式:
[Task][State]Handler- UITaskFinishedHandler
[Task][State]Handler- UITaskFinishedHandler
[Event]Handler- ControlLoadedHandler
[Event]Handler- ControlLoadedHandler
[Function Name]Delegate- DoSomeWorkDelegate- used when I need to create a delegate for calling a function on a different/new thread
[Function Name]Delegate- DoSomeWorkDelegate- 当我需要创建一个委托以在不同/新线程上调用函数时使用
[Task]Callback- ContainerLoadedCallback - used when control A starts an action which control B does most of the work and control A has passed a dependency in to control B (i.e. ControlA may have passed a UI container for ControlB to fill and needs notification to actually show the container)
[Task]Callback- ContainerLoadedCallback- 当控件 A 开始一个动作时使用,控件 B 完成大部分工作并且控件 A 已将依赖项传递给控件 B(即 ControlA 可能已传递一个 UI 容器供 ControlB 填充并需要通知以实际显示容器)
When you have a project that uses a lot of multi threading or async WCF calls you can end up with a lot of delegates floating around, so it is important to adopt a standard that at least makes sense to you.
当您的项目使用大量多线程或异步 WCF 调用时,您最终可能会遇到大量委托,因此采用至少对您有意义的标准很重要。
回答by Borislav Ivanov
Microsoft's Framework Design Guidelines- the naming almanac for me, says the followingon the topic:
微软的框架设计指南——我的命名年鉴,对这个话题做了以下说明:
√ DO add the suffix "EventHandler" to names of delegates that are used in events.
√ DO add the suffix "Callback" to names of delegates other than those used as event handlers.
X DO NOT add the suffix "Delegate" to a delegate.
√ 务必在事件中使用的委托名称中添加后缀“EventHandler”。
√ 务必将后缀“Callback”添加到除用作事件处理程序的委托之外的委托名称。
X 不要给委托添加后缀“Delegate”。
回答by Kevin Kibler
Since a delegate is something that performs an action (a verb), the delegate should be named what you would call something that performs that action. Take Converter<TInput, TOutput>for example. The verb is Convert. The thing that does the converting is called a converter, hence the name of the delegate.
由于委托是执行动作(动词)的东西,因此委托的名称应该是您所称的执行该动作的东西。以Converter<TInput, TOutput>为例。动词是Convert。进行转换的东西称为转换器,因此得名委托。
回答by Aaronaught
This depends on a few things.
这取决于几件事。
If the delegate is going to be used as an event, it should alwaysbe referred to as an EventHandlersubtype, for example:
如果委托将用作事件,则应始终将其称为EventHandler子类型,例如:
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 againstincluding words like "delegate" or "handler" in the delegate name, exceptin the special case of EventHandlertypes.
如果这不是事件,那么 MS 编码指南(我似乎永远无法在 Google 上找到正确的副本)明确建议不要在委托名称中包含诸如“委托”或“处理程序”之类的词,除非是特殊情况EventHandler类型。
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).
通常,委托应该以actions命名,就像ValueExtracting(如果委托发生在值被提取之前)或ValueExtracted(在提取之后)。
The Func<T1, T2, ..., TResult>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:
该Func<T1, T2, ..., TResult>委托语法也变得越来越普遍,但除非你有4个或多个参数往里走,你并不需要在所有声明自己-只需使用现有的:
object ExtractObject(object source, Func<object, object> 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.
大多数委托用法都属于上述类别之一,因此请适当地确定它用于哪个类别。
回答by MikeP
I never thought about it, mostly because I just use one of the EventHandler<T>, Func<T>, or Action<T>overloads and never bother defining my own. I would probably pick ValueExtractor from those you've listed. This makes it sound more like an object, and when you invoke it you'll be using that object to perform an action. For example:
我从来没有想过这个问题,主要是因为我只使用一个EventHandler<T>,Func<T>或Action<T>过载,从不打扰我定义自己。我可能会从您列出的那些中选择 ValueExtractor。这使它听起来更像是一个对象,当您调用它时,您将使用该对象来执行操作。例如:
ValueExtractor extractor += Blah;
var value = extractor(data);
Additionally, most of the built-in delegates are named like nouns as well. When in doubt, follow the .NET framework.
此外,大多数内置委托也像名词一样命名。如有疑问,请遵循 .NET 框架。
回答by Matias
I would go with ValueExtraction..
I've Never thought why, but I guess because you're storing an operation and it should be a noun..
strictly this is not an operation, I know...
我会选择 ValueExtraction ..
我从来没有想过为什么,但我猜是因为你正在存储一个操作,它应该是一个名词......严格来说,这不是一个操作,我知道......
回答by Sam Harwell
Based on Enumerable.Sum, I'd pass the delegate as a Func<object, object>and name the parameter selector:
基于Enumerable.Sum,我将委托作为 a 传递Func<object, object>并命名参数selector:
void Foo(Func<object, object> selector) ...
If you have to make your own delegate for it, I'd go with ValueExtractorsince that's the most descriptive name for what it does.
如果你必须为它制作自己的委托,我会去,ValueExtractor因为这是它所做的最具描述性的名称。

