C# 您发现扩展方法的哪些优点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/487904/
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
What Advantages of Extension Methods have you found?
提问by Jerry
A "non-believer" of C# was asking me what the purpose to extension methods was. I explained that you could then add new methods to objects that were already defined, especially when you don't own/control the source to the original object.
C# 的“非信徒”问我扩展方法的目的是什么。我解释说,您可以向已经定义的对象添加新方法,尤其是当您不拥有/控制原始对象的源时。
He brought up "Why not just add a method to your own class?" We've been going round and round (in a good way). My general response is that it is another tool in the toolbelt, and his response is it is a useless waste of a tool... but I thought I'd get a more "enlightened" answer.
他提出了“为什么不直接在自己的类中添加一个方法?” 我们一直在走来走去(以一种好的方式)。我的一般反应是它是工具带中的另一个工具,而他的反应是这是对工具的无用浪费……但我想我会得到一个更“开明”的答案。
What are some scenarios that you've used extension methods that you couldn't have (or shouldn't have) used a method added on to your own class?
在哪些情况下,您使用了无法(或不应该)使用添加到您自己的类中的方法的扩展方法?
采纳答案by Eduardo Campa?ó
I think extension methods help a lot when writing code, if you add extension methods to basic types you'll get them quicky in the intellisense.
我认为扩展方法在编写代码时有很大帮助,如果您向基本类型添加扩展方法,您将在智能感知中快速获得它们。
I have a format provider to format a file size. To use it I need to write:
我有一个格式提供程序来格式化文件大小。要使用它,我需要写:
Console.WriteLine(String.Format(new FileSizeFormatProvider(), "{0:fs}", fileSize));
Creating an extension method I can write:
创建一个我可以写的扩展方法:
Console.WriteLine(fileSize.ToFileSize());
Cleaner and simpler.
更干净更简单。
回答by Tamir
I have only one word to tell about it: MAINTAINABILITYthis is the key for extension methods use
我只有一个词要说:可维护性这是扩展方法使用的关键
回答by Brian
Don't forget tooling! When you add an extension method M on type Foo, you get 'M' in Foo's intellisense list (assuming the extension class is in-scope). This make 'M' much easier to find than MyClass.M(Foo,...).
不要忘记工具!当您在 Foo 类型上添加扩展方法 M 时,您会在 Foo 的智能感知列表中获得“M”(假设扩展类在范围内)。这使得 'M' 比 MyClass.M(Foo,...) 更容易找到。
At the end of the day, it's just syntactic sugar for elsewhere-static-methods, but like buying a house: 'location, location, location!' If it hangs on the type, people will find it!
归根结底,这只是其他地方静态方法的语法糖,但就像买房子一样:“位置,位置,位置!” 如果它挂在类型上,人们会找到它!
回答by Ray Booysen
One of the great reasons for using extension methods is LINQ. Without extension methods a lot of what you can do in LINQ would be very hard. The Where(), Contains(), Select extension methods means a lot more functionality is added to existing types without changing their structure.
使用扩展方法的重要原因之一是 LINQ。如果没有扩展方法,您在 LINQ 中可以做的很多事情将非常困难。Where()、Contains()、Select 扩展方法意味着在不改变其结构的情况下向现有类型添加了更多功能。
回答by Bluenuance
My personal argument for Extension methods is, they fit very well into an OOP design: consider the simple method
我个人对扩展方法的论点是,它们非常适合 OOP 设计:考虑简单的方法
bool empty = String.IsNullOrEmpty (myString)
in comparison to
相比
bool empty = myString.IsNullOrEmpty ();
回答by Andrew Hare
The onlyadvantage of extension methods is code readability. That's it.
扩展方法的唯一优势是代码可读性。就是这样。
Extension methods allow you to do this:
扩展方法允许你这样做:
foo.bar();
instead of this:
而不是这个:
Util.bar(foo);
Now there are a lot of things in C# that are like this. In other words there are many features in C# that seem trivial and don't have great benefit in and of themselves. However once you begin combining these features together you begin to see something just a bit bigger than the sum of its parts. LINQ benefits greatly from extension methods as LINQ queries would be almost unreadable without them. LINQ would be possiblewithout extension methods, but not practical.
现在C#中有很多东西是这样的。换句话说,C# 中有许多功能看起来微不足道,而且它们本身并没有很大的好处。但是,一旦您开始将这些功能组合在一起,您就会开始看到比其各部分之和大一点的东西。LINQ 从扩展方法中受益匪浅,因为没有它们,LINQ 查询几乎不可读。LINQ将是可能的,而不扩展方法,但不实用。
Extension methods are a lot like C#'s partial classes. By themselves they are not very helpful and seem trivial. But when you start working with a class that needs generated code, partial classes start to make a lot more sense.
扩展方法很像 C# 的部分类。它们本身并不是很有帮助,而且看起来微不足道。但是当您开始使用需要生成代码的类时,部分类开始变得更有意义。
回答by cgreeno
Fluent Interfaces and Context Sensitivityas demonstrated by Greg Young on CodeBetter
Greg Young 在CodeBetter上展示的流畅界面和上下文敏感性
回答by DavGarcia
It allows C# to better support dynamic languages, LINQ and a dozen other things. Check out Scott Guthrie's article.
它允许 C# 更好地支持动态语言、LINQ 和其他十几种东西。查看Scott Guthrie 的文章。
回答by Schildmeijer
Its true that you can add your (extension) method directly into your class. But not all classes are written by you. Classes from the core library or third party libraries are often closed and it would be impossible to get the syntatic sugar without extension methods. But remember, extension methods are just like (static) standalone methods in eg. c++
确实可以将(扩展)方法直接添加到类中。但并不是所有的类都是你写的。来自核心库或第三方库的类通常是封闭的,如果没有扩展方法,就不可能获得语法糖。但请记住,扩展方法就像 eg. 中的(静态)独立方法。C++
回答by Jason Punyon
Extension methods are really the .NET incorporation of the "Introduce Foreign Method"refactor from Martin Fowler's Book (down to the method signature). They come with basically the same benefits and pitfalls. In the section on this refactor he says that they're a work-around for when you can't modify the class that should really own the method.
扩展方法实际上是Martin Fowler 的书中的“引入外部方法”重构(直到方法签名)的 .NET 合并。它们具有基本相同的好处和缺陷。在有关此重构的部分中,他说当您无法修改应该真正拥有该方法的类时,它们是一种解决方法。