[C# FAQ]const和static readonly的区别
2011-08-16 10:05
204 查看
名词解释
const:该关键字用于修改字段或局部变量的声明。 它指定字段或局部变量的值是常数,不能被修改。const必须在声明时赋值,对于引用类型的常数,可能的值只能是 string 和 null。
你还可以像这样赋值:
// 一次给多个常量赋值
public const double x = 1.0, y = 2.0, z = 3.0;
// 用常量参与常量赋值
public const int c1 = 5;
public const int c2 = c1 + 100;
但以下赋值方法是不允许的。
// 调用一个函数来获得值:
const int i = GetValue();
// 涉及到了构造函数:
const SomeClass myObj = new SomeClass();
ReadOnly:该声明引入的字段赋值只能作为声明的一部分出现,或者出现在同一类的构造函数中。
class Age
{
readonly int _year;
Age(int year)
{
_year = year;
}
void ChangeYear()
{
//_year = 1967; // Compile error if uncommented.
}
}
Static ReadOnly:用于声明静态常量,声明的对象只能在初始化或者构造函数中赋值。
对比分析
我们都知道,const和static readonly的确很像:通过类名而不是对象名进行访问,在程序中只读等等。在多数情况下可以混用。二者本质的区别在于,const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值。而static readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值。
明白了这个本质区别,我们就不难看出下面的语句中static readonly和const能否互换了:
1. static readonly MyClass myins = new MyClass();
2. static readonly MyClass myins = null;
3. static readonly A = B * 20;
static readonly B = 10;
4. static readonly int [] constIntArray = new int[]{1, 2, 3};
5. void SomeFunction()
{
const int a = 10;
...
}
1:不可以换成const。new操作符是需要执行构造函数的,所以无法在编译期间确定
2:可以换成const。我们也看到,Reference类型的常量(除了String)只能是Null。
3:可以换成const。我们可以在编译期间很明确的说,A等于200。
4:不可以换成const。道理和1是一样的,虽然看起来1,2,3的数组的确就是一个常量。
5:不可以换成readonly,readonly只能用来修饰类的field,不能修饰局部变量,也不能修饰property等其他类成员。
因此,对于那些本质上应该是常量,但是却无法使用const来声明的地方,可以使用static readonly。例如C#规范中给出的例子:
public class Color
{
public static readonly Color Black = new Color(0, 0, 0);
public static readonly Color White = new Color(255, 255, 255);
public static readonly Color Red = new Color(255, 0, 0);
public static readonly Color Green = new Color(0, 255, 0);
public static readonly Color Blue = new Color(0, 0, 255);
private byte red, green, blue;
public Color(byte r, byte g, byte b)
{
red = r;
green = g;
blue = b;
}
}
static readonly需要注意的一个问题是,对于一个static readonly的Reference类型,只是被限定不能进行赋值(写)操作而已。而对其成员的读写仍然是不受限制的。
public static readonly MyClass myins = new MyClass();
…
myins.SomeProperty = 10;//正常
myins = new MyClass();//出错,该对象是只读的
但是,如果上例中的MyClass不是一个class而是一个struct,那么后面的两个语句就都会出错。
小结
Const的好处能够带来稍许的性能上的提升。
能够给一些“神奇”的值命名。在程序中出现的数值等“神奇”的数值,用Const替换会使程序更容易阅读。
Const的坏处
版本问题。如果A.exe调用了B.dll中定义的const常量,那么如果重新调整const常量时,A.exe中获取的常量值仍然是修改前的内容。更多看这里。
readonly的好处
在初始化和构造函数前可以赋值,可以使用方法为其赋值。
使用场合更多。
相关文章推荐
- C# FAQ: const和static readonly有什么区别?
- C# FAQ: const和static readonly有什么区别?
- C# FAQ: const和static readonly有什么区别?
- C# FAQ: const和static readonly有什么区别?
- C# FAQ: const和static readonly有什么区别?
- c# 中const 和 static readonly 的区别
- C#中const与 static readonly 区别
- C#老生常谈之const与static readonly只区别
- C#中const与static readonly的区别
- C#中const与 static readonly 区别
- C#中静态常量(const)和动态常量(static readonly)的一些区别
- C#Readonly, static, const 区别<转载>
- C# static readonly 与 const 的区别
- C#中Const与Static Readonly的区别
- C#中的const与static readonly区别
- C# Const和Static ReadOnly区别(转)
- C#中const与 static readonly 区别(转)
- C#中的const与static readonly区别
- C#中const和static readonly 的区别
- const 和 static readonly 区别?