C# 扩展方法 vs. Helper 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11330384/
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
Extension Method vs. Helper Class
提问by Matthew Layton
Possible Duplicate:
Extension Methods vs Static Utility Class
可能的重复:
扩展方法与静态实用程序类
I am building an API of general functions which perform actions based upon objects in .NET. For example; I have created a function that checks a string to see if it is an email address.
我正在构建一个通用函数的 API,它根据 .NET 中的对象执行操作。例如; 我创建了一个函数来检查字符串以查看它是否是电子邮件地址。
I could either have:
我可以有:
static bool IsEmailAddress(string text)
{
return IsMail(text);
}
or I could create an extension method that would be used like so:
或者我可以创建一个像这样使用的扩展方法:
string text = "[email protected]";
if (text.IsEmailAddress())
{
}
which is more suitable, or do you think since this is a general purpose library, I could technically implement it both ways and allow the developer to decide which is best for them?
哪个更合适,或者你认为因为这是一个通用库,我可以在技术上以两种方式实现它并允许开发人员决定哪个最适合他们?
采纳答案by Steven
Creating an extension method means that it will automatically show up during intellisense when a user uses that type. You have to be carefull not adding a lot of noise to the list of methods developers browse (especially when creating a reusable framework). For instance, when those methods are just usable in a certain context, you are probably better of using 'normal' static methods. Especially when implementing extension methods for general types such as string.
创建扩展方法意味着当用户使用该类型时,它会在智能感知期间自动显示。您必须小心不要在开发人员浏览的方法列表中添加太多干扰(尤其是在创建可重用框架时)。例如,当这些方法仅在特定上下文中可用时,您可能最好使用“普通”静态方法。特别是在为一般类型实现扩展方法时,例如string.
Take for instance an ToXml(this string)extension method, or an ToInt(this string)extension methods. Although it seems pretty convenient to have these extension methods, converting text to XML is not something you will do throughout the application and it would be as easy to have do XmlHelper.ToXml(someString).
以ToXml(this string)扩展方法或ToInt(this string)扩展方法为例。虽然拥有这些扩展方法看起来很方便,但将文本转换为 XML 并不是您在整个应用程序中都会做的事情,而且会很容易做到XmlHelper.ToXml(someString)。
There is only one thing worse, and that is adding an extension method on object.
只有一件事更糟糕,那就是在object.
If you're writing an reusable framework, the book Framework-Design-Guidelines by Krzysztof Cwalinais an absolute must read.
如果您正在编写一个可重用的框架,那么 Krzysztof Cwalina 所著的 Framework-Design-Guidelines一书绝对是必读的。
回答by Aghilas Yakoub
I prefer Extension Method, because your code is elegant, and you can define an extension method on a sealed class of the framework.
我更喜欢扩展方法,因为你的代码很优雅,你可以在框架的密封类上定义扩展方法。
回答by MikeSW
An extension method is automatically part of a static class. This means that the consumer can use either the extension methods or call the static method from the class if she wants it. I use extension methods as much as I can, they are easier to discover if they're put in the proper namespace.
扩展方法自动成为静态类的一部分。这意味着消费者可以使用扩展方法,也可以根据需要从类中调用静态方法。我尽可能多地使用扩展方法,如果将它们放在正确的命名空间中,则更容易发现它们。
回答by Jacek Gorgoń
Extension methods allow developers to not be aware exactly what the helper class is called and where it is located, not to mention the very fact of its existence. Do note, that you still need to put their namespace in usingclause - perhaps place them in some common, top-level namespace for your application.
扩展方法允许开发人员不知道助手类的确切名称和位置,更不用说它存在的事实了。请注意,您仍然需要将它们的命名空间放在using子句中 - 也许将它们放在应用程序的一些常见的顶级命名空间中。
回答by banging
The question is which .NET Framework will you target? If < 3.5 then extension methods are not available. Otherwise, why would you create a new class?
问题是您将针对哪个 .NET Framework?如果 < 3.5,则扩展方法不可用。否则,你为什么要创建一个新类?

