扩展方法的使用方法

由网友(繁华落尽、无言)分享简介:在什么时候使用扩展方法有意义吗?在是否加入扩展方法,一种影响性能?When does using extension methods make sense?Does adding extension methods to a type affect performance?这些问题是跟进的问题我刚才问了一下扩展方...

在什么时候使用扩展方法有意义吗? 在是否加入扩展方法,一种影响性能?

When does using extension methods make sense? Does adding extension methods to a type affect performance?

这些问题是跟进的问题我刚才问了一下扩展方法。

These questions are follow up to the question i asked earlier about Extension Methods.

推荐答案

这些道理,当您使用LINQ和想要链或管道输出功能从一个功能到另一个。它提高了code的可读性,让你的前preSS概念更优雅(不管它是什么值得)。

When does using extension methods make sense?

They make sense when you are using LINQ and want to chain or pipe functional output from one function to another. It improves the readability of the code and allows you to express a concept more elegantly (whatever that is worth).

他们还让你得到任何你喜欢不修改该类型的源类型实例方法的出现,这往往可以帮助可读性和你的code前pressiveness当它被合理使用

They also allow you to give the appearance of instance methods on any type you like without modifying the source of that type, this can often help readability and the expressiveness of your code when it is used reasonably

请注意,一个扩展方法调用,如:

Note that an extension method call like:

instance.SomeExtensionMethod()

被编译为:

StaticExtensionMethodClass.SomeExtensionMethod(instance);

所以性能会与任何其他静态方法的调用。

so performance will be the same as any other static method call.

阅读全文

相关推荐

最新文章