扩展方法的使用 [英] Usage of Extension Methods

查看:120
本文介绍了扩展方法的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  • 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.

    这篇关于扩展方法的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆