接口扩展方法的C#命名约定

时间:2022-09-02 10:05:47

I typically name my C# interfaces as IThing. I'm creating an extension method class for IThing, but I don't know what to name it. On one hand, calling it ThingExtensions seems to imply it is an extension class to some Thing class instead of to the IThing interface. It also makes the extension class be sorted away from the interface it extends, when viewing files alphabetically. On the other hand, naming it IThingExtensions makes it look like it is an interface itself, instead of an extension class for an interface. What would you suggest?

我通常将我的C#接口命名为IThing。我正在为IThing创建一个扩展方法类,但我不知道该命名它。一方面,将其称为ThingExtensions似乎意味着它是某些Thing类的扩展类而不是IThing接口。当按字母顺序查看文件时,它还使扩展类与其扩展的接口分开。另一方面,将其命名为IThingExtensions使其看起来像是一个接口本身,而不是接口的扩展类。你会建议什么?

Edit: there is not a Thing class that implements IThing, in response to some of the comments.

编辑:没有一个Thing类实现IThing,以响应一些注释。

6 个解决方案

#1


22  

I definitely prefer the name ThingExtensions over IThingExtensions. The reason being that to most programmers an I prefix on a type implies that it is an interface. This is both a very common pattern and part of the .Net Design Guidelines.

我绝对更喜欢名称ThingExtensions而不是IThingExtensions。原因是大多数程序员在类型上使用I前缀意味着它是一个接口。这是一种非常常见的模式,也是.Net设计指南的一部分。

Adding an I prefix for the extension method case breaks both assumptions and established guidelines.

为扩展方法案例添加I前缀会破坏假设和既定准则。

There is also precedence for this in the Base Class Library. The majority of the extension methods available for IEnumerable are contained in the type Enumerable.

基类库中也有优先权。 IEnumerable可用的大多数扩展方法都包含在Enumerable类型中。

#2


14  

I, personally, would use IThingExtensions.

我个人会使用IThingExtensions。

From a usability standpoint, the end user never sees this class - they only include it's namespace. If the namespace is the same as IThing, then it doesn't matter - they'll already have it.

从可用性的角度来看,最终用户永远不会看到这个类 - 它们只包含它的命名空间。如果名称空间与IThing相同,那么无关紧要 - 他们已经拥有它。

That being said, I think that the fact these are extensions for any IThing makes IThingExtensions the most clear. If you have a Thing class, calling this ThingExtensions may seem ambiguous (are you extending the interface or the implementation itself?).

话虽如此,我认为这些是任何IThing的扩展,这使得IThingExtensions变得最清晰。如果你有一个Thing类,调用这个ThingExtensions可能看起来很模糊(你是扩展接口还是实现本身?)。

That being said, the framework uses a very different approach. The framework's approach is to use a class named Thing to extend IThing. For examples, see Enumerable (extending IEnumerable) and Queryable (extending IQueryable). This would also be a very good option.

话虽如此,该框架使用了一种非常不同的方法。框架的方法是使用名为Thing的类来扩展IThing。例如,请参阅Enumerable(扩展IEnumerable)和Queryable(扩展IQueryable)。这也是一个非常好的选择。

#3


3  

Most programmers I know put all of their extension methods for an application in a static class called ExtensionMethods (or something like that), regardless of the class the extensions modify, and then they put this class into their main program namespace.

我所知道的大多数程序员都将应用程序的所有扩展方法放在一个名为ExtensionMethods(或类似的东西)的静态类中,而不管扩展修改的类,然后将这个类放入其主程序命名空间。

Their rationale is that, if you put the extension method in the same namespace as the class it modifies, you can confuse the method with the methods that are part of the actual class, which suggests that the extension method is part of the original functionality when it isn't.

它们的基本原理是,如果将扩展方法放在与它修改的类相同的命名空间中,则可以将该方法与作为实际类的一部分的方法混淆,这表明扩展方法是原始功能的一部分。事实并非如此。

Their isn't universal agreement on this, of course. See here: How do you manage the namespaces of your extension methods?

当然,他们不是就此达成普遍协议。请参阅此处:如何管理扩展方法的命名空间?

#4


1  

I've taken a slightly different approach, and rather than suffix with Extension reversed the naming strategy to prefix the class containing the extension methods with Extend; my rationale is as follows:

我采用了稍微不同的方法,而不是使用Extension的后缀反转命名策略,以使用Extend为包含扩展方法的类添加前缀;我的理由如下:

  • As pointed out in Reed Copsey's answer, very (very) seldom will client code reference the containing class directly as the very point of extension methods is to emulate reference methods. They won't see the classes, so your class naming convention choice should have insignificant impact.
  • 正如Reed Copsey的回答所指出的,非常(非常)很少会将客户端代码直接引用包含类,因为扩展方法的最重要的方法是模拟引用方法。他们不会看到这些课程,因此您的课程命名惯例选择应该产生微不足道的影响。
  • The classes must be static, and thus you'll never instantiate them. Therefore you never end up with the semantic oddity of new ExtendThing() as far as naming is concerned.
  • 类必须是静态的,因此您永远不会实例化它们。因此,就命名而言,你永远不会得到新的ExtendThing()的语义怪异。
  • All of your Extend* classes are visually grouped with alpha file sorting.
  • 所有Extend *类都通过alpha文件排序进行可视化分组。
  • And with respect to your question specifically, there isn't any interface naming prefix confusion; you can have ExtendThing and ExtendIThing and (IMO) their intent and target is clear.
  • 而且就你的问题而言,没有任何界面命名前缀混淆;您可以使用ExtendThing和ExtendIThing以及(IMO)他们的意图和目标是明确的。

namespace MyCompany.Extensions
{
    public static class ExtendObject { }

    public static class ExtendDateTime { }

    public static class ExtendIEnumerable { }
}

#5


0  

I would prefer putting it in a folder (and namespace) called Extensions and naming it IThingExtensions.

我更喜欢将它放在名为Extensions的文件夹(和命名空间)中,并将其命名为IThingExtensions。

#6


0  

I am not aware of any standard convention for this. I would use either ThingExtensions or ThingInterfaceExtensions. I would stay away from IThingExtensions as you also suggested.

我不知道有任何标准惯例。我会使用ThingExtensions或ThingInterfaceExtensions。你也建议我远离IThingExtensions。

#1


22  

I definitely prefer the name ThingExtensions over IThingExtensions. The reason being that to most programmers an I prefix on a type implies that it is an interface. This is both a very common pattern and part of the .Net Design Guidelines.

我绝对更喜欢名称ThingExtensions而不是IThingExtensions。原因是大多数程序员在类型上使用I前缀意味着它是一个接口。这是一种非常常见的模式,也是.Net设计指南的一部分。

Adding an I prefix for the extension method case breaks both assumptions and established guidelines.

为扩展方法案例添加I前缀会破坏假设和既定准则。

There is also precedence for this in the Base Class Library. The majority of the extension methods available for IEnumerable are contained in the type Enumerable.

基类库中也有优先权。 IEnumerable可用的大多数扩展方法都包含在Enumerable类型中。

#2


14  

I, personally, would use IThingExtensions.

我个人会使用IThingExtensions。

From a usability standpoint, the end user never sees this class - they only include it's namespace. If the namespace is the same as IThing, then it doesn't matter - they'll already have it.

从可用性的角度来看,最终用户永远不会看到这个类 - 它们只包含它的命名空间。如果名称空间与IThing相同,那么无关紧要 - 他们已经拥有它。

That being said, I think that the fact these are extensions for any IThing makes IThingExtensions the most clear. If you have a Thing class, calling this ThingExtensions may seem ambiguous (are you extending the interface or the implementation itself?).

话虽如此,我认为这些是任何IThing的扩展,这使得IThingExtensions变得最清晰。如果你有一个Thing类,调用这个ThingExtensions可能看起来很模糊(你是扩展接口还是实现本身?)。

That being said, the framework uses a very different approach. The framework's approach is to use a class named Thing to extend IThing. For examples, see Enumerable (extending IEnumerable) and Queryable (extending IQueryable). This would also be a very good option.

话虽如此,该框架使用了一种非常不同的方法。框架的方法是使用名为Thing的类来扩展IThing。例如,请参阅Enumerable(扩展IEnumerable)和Queryable(扩展IQueryable)。这也是一个非常好的选择。

#3


3  

Most programmers I know put all of their extension methods for an application in a static class called ExtensionMethods (or something like that), regardless of the class the extensions modify, and then they put this class into their main program namespace.

我所知道的大多数程序员都将应用程序的所有扩展方法放在一个名为ExtensionMethods(或类似的东西)的静态类中,而不管扩展修改的类,然后将这个类放入其主程序命名空间。

Their rationale is that, if you put the extension method in the same namespace as the class it modifies, you can confuse the method with the methods that are part of the actual class, which suggests that the extension method is part of the original functionality when it isn't.

它们的基本原理是,如果将扩展方法放在与它修改的类相同的命名空间中,则可以将该方法与作为实际类的一部分的方法混淆,这表明扩展方法是原始功能的一部分。事实并非如此。

Their isn't universal agreement on this, of course. See here: How do you manage the namespaces of your extension methods?

当然,他们不是就此达成普遍协议。请参阅此处:如何管理扩展方法的命名空间?

#4


1  

I've taken a slightly different approach, and rather than suffix with Extension reversed the naming strategy to prefix the class containing the extension methods with Extend; my rationale is as follows:

我采用了稍微不同的方法,而不是使用Extension的后缀反转命名策略,以使用Extend为包含扩展方法的类添加前缀;我的理由如下:

  • As pointed out in Reed Copsey's answer, very (very) seldom will client code reference the containing class directly as the very point of extension methods is to emulate reference methods. They won't see the classes, so your class naming convention choice should have insignificant impact.
  • 正如Reed Copsey的回答所指出的,非常(非常)很少会将客户端代码直接引用包含类,因为扩展方法的最重要的方法是模拟引用方法。他们不会看到这些课程,因此您的课程命名惯例选择应该产生微不足道的影响。
  • The classes must be static, and thus you'll never instantiate them. Therefore you never end up with the semantic oddity of new ExtendThing() as far as naming is concerned.
  • 类必须是静态的,因此您永远不会实例化它们。因此,就命名而言,你永远不会得到新的ExtendThing()的语义怪异。
  • All of your Extend* classes are visually grouped with alpha file sorting.
  • 所有Extend *类都通过alpha文件排序进行可视化分组。
  • And with respect to your question specifically, there isn't any interface naming prefix confusion; you can have ExtendThing and ExtendIThing and (IMO) their intent and target is clear.
  • 而且就你的问题而言,没有任何界面命名前缀混淆;您可以使用ExtendThing和ExtendIThing以及(IMO)他们的意图和目标是明确的。

namespace MyCompany.Extensions
{
    public static class ExtendObject { }

    public static class ExtendDateTime { }

    public static class ExtendIEnumerable { }
}

#5


0  

I would prefer putting it in a folder (and namespace) called Extensions and naming it IThingExtensions.

我更喜欢将它放在名为Extensions的文件夹(和命名空间)中,并将其命名为IThingExtensions。

#6


0  

I am not aware of any standard convention for this. I would use either ThingExtensions or ThingInterfaceExtensions. I would stay away from IThingExtensions as you also suggested.

我不知道有任何标准惯例。我会使用ThingExtensions或ThingInterfaceExtensions。你也建议我远离IThingExtensions。