扩展方法:使用太多时出现性能问题?
可能的重复:
扩展方法性能
当我使用过多的扩展方法时,我会遇到性能问题吗?
举个例子:
假设我在字符串类型上有 100 个扩展方法,并且业务对象有 50 个字符串属性。 现在我创建这个业务对象的集合,也许有 500 个项目?
这些字符串扩展方法对 RAM、CPU 等有影响吗?!
我真的很喜欢扩展方法,但我想知道它的使用是否有限制。
Possible Duplicate:
Extension Method Performance
Will I face performance issues when I´m using extension methods too heavy in any way?
Just an example:
Lets say I´ve 100 extension methods on the type string and a business object that has 50 string properties.
Now I create a collection of this business object, maybe 500 items?
Do these extension methods on string have any impact to RAM, CPU, ...?!
I really like extension methods but I´d like to know if there is a limitation concerning its usage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,你不会。
扩展方法只是带有一些语法糖的普通静态方法。尽可能多地使用它们:)
No you will not.
Extension methods are just normal static methods with some syntactic sugar. Use them as much as you want :)
您可能会减慢 IDE 中的智能感知速度,并可能使智能感知列表有点笨拙,但与任何其他静态方法相比,它不会对代码本身的执行产生任何影响。
You might slow intellisense down in the IDE - and possibly make the intellisense lists a bit unwieldy, but it won't have any impact on the execution of the code itself over any other static method.
扩展方法是静态类的一部分,它是一种语法糖,因为模拟某些静态方法是其他类的实例方法。
总之,这里没有性能损失。它是相同的
ExtensionMethods.YourExtensionMethod(objectToAddMethod)
和objectToAddMethod.YourExtensionMethod()
。An extension method is part of a static class, and it's a syntactic sugar because simulates that some static method is an instance method of some other class.
In conclusion, there's no performance penalty here. It's the same
ExtensionMethods.YourExtensionMethod(objectToAddMethod)
andobjectToAddMethod.YourExtensionMethod()
.我认为这是重复的:
请参阅:创建运行对象类型的扩展方法是否会对性能造成影响?
那里的答案很好地解释了这一点!
I think this is a duplicate:
See: Is there a performance hit for creating Extension methods that operate off the object type?
The answer there explains this very well!