使用常量,而不是变量里面方法的优点 [英] Advantages of using const instead of variables inside methods

查看:160
本文介绍了使用常量,而不是变量里面方法的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我有一个方法的局部变量,ReSharper的建议将它们转换为常量:

Whenever I have local variables in a method, ReSharper suggests to convert them to constants:

// instead of this:
var s = "some string";
var flags = BindingFlags.Public | BindingFlags.Instance;

// ReSharper suggest to use this:
const string s = "some string";
const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;



由于这些是真正的常量值(而不是变量)据我所知,ReSharper的建议将其更改为常量。

Given that these are really constant values (and not variables) I understand that ReSharper suggest to change them to const.

但除此之外,有没有这证明使用任何其他好处中使用const(如更好的性能)常量的BindingFlags 而不是方便性和可读性 VAR 关键字

But apart from that, is there any other advantage when using const (e.g. better performance) which justifies using const BindingFlags instead of the handy and readable var keyword?

BTW:我刚刚发现了类似的问题在这里: ReSharper的建议总是我做常量字符串代替字符串。,但我认为这更多的是一类在我的问题是关于局部变量/ consts的领域。

BTW: I just found a similar question here: Resharper always suggesting me to make const string instead of string., but I think it is more about fields of a class where my question is about local variable/consts.

推荐答案

如果您尝试将值赋给一个常数,从而可能阻止您意外更改,编译器将抛出一个错误。

The compiler will throw an error if you try to assign a value to a constant, thus possibly preventing you from accidentally changing it.

另外,通常是有小的性能好处是用常量与变量。这有做与被编译成MSIL的方式,每此MSDN杂志Q&安培; A

Also, usually there is a small performance benefit to using constants vs. variables. This has to do with the way they are compiled to the MSIL, per this MSDN magazine Q&A:

现在,无论在敏代码引用,而不必做ldloc.0来获得的价值,变量中,MSIL只是加载它是硬编码到MSIL的恒定值。 因此,通常有一个小的性能和内存优势,使用常量。然而,为了使用它们,你必须在编译时变量的值,并在编译时这个常数的任何引用即使他们在不同的装配,才会有这种替代的。

Now, wherever myInt is referenced in the code, instead of having to do a "ldloc.0" to get the value from the variable, the MSIL just loads the constant value which is hardcoded into the MSIL. As such, there's usually a small performance and memory advantage to using constants. However, in order to use them you must have the value of the variable at compile time, and any references to this constant at compile time, even if they're in a different assembly, will have this substitution made.

常量肯定是一个有用的工具,如果你知道在编译时的值。如果你不这样做,但要确保您的变量只设置一次,就可以使用C#中的只读关键字(映射在MSIL到initonly),以表示该变量的值只能在构造函数中进行设置;在这之后,它改变它的错误。这通常用于当一个字段帮助确定一类的身份,并且通常设置为等于一个构造符参数

Constants are certainly a useful tool if you know the value at compile time. If you don't, but want to ensure that your variable is set only once, you can use the readonly keyword in C# (which maps to initonly in MSIL) to indicate that the value of the variable can only be set in the constructor; after that, it's an error to change it. This is often used when a field helps to determine the identity of a class, and is often set equal to a constructor parameter.

这篇关于使用常量,而不是变量里面方法的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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