Is it true that using “this.” before the parameters in c# uses more memory?

半城伤御伤魂 提交于 2019-12-12 18:20:56

问题


than just to call the parameter as it is?


回答1:


If you mean fields, then no. The compiler injects "this" (ldarg.0) whether you use it explicitly (this.foo) or implicitly (foo).

It does, however, take 5 more characters in your source code... so a handful of bytes on your development hard disk. It will make exactly zero difference in the compiled IL or at runtime.

There are two scenarios where use of "this" changes things:

  • when there is a variable/parameter with the same name (this.foo = foo;)
  • when resolving extension methods (this.SomeMethod();)



回答2:


Your question is much too ambiguous to answer definitively but i would still start with a resounding No

then i'd want to know what exactly do you mean with parameter? I would normally interpret it as "argument to a method" but they are not tied to "this" within scope so you probably meant "members" such as fields, properties and/or methods.

If all of my assumptions about how to interpret your question are correct, I stand by my former "No".

But i would like to know where you got that idea from.




回答3:


I do not know, if it uses more memory, but I don't think so, its only a clear reference, something that would be done under the hood as well by the compiler.




回答4:


I guess you mean before the variable name? I do not see why it would use more memory. The CLR must be optimize whatever the syntax is to refer to the variable to a way that it doesn't affect the performance (after verification, it will add the this...). So no it doesn't.



来源:https://stackoverflow.com/questions/352720/is-it-true-that-using-this-before-the-parameters-in-c-sharp-uses-more-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!