static readonly 和 const的区别和联系
2010-05-24 16:56
399 查看
以下的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;
}
const和static readonly很像:通过类名而不是对象名进行访问,在程序中只读等等。在多数情况下可以混用。二者本质的区别在于:
* const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值。
* static readonly是在运行时计算出其值的, 只有两种途径为其赋值,在声明该变量的时候或在默认的静态构造函数里面为其赋值。实际上这两种方法最后生成的IL代码是相同的(都是在静态构造函数中赋值)。
明白了这个本质区别,我们就不难看出下面的语句中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,那么后面的两个语句就都会出错。
static readonly的field可以被反射修改。
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(A.field1);
FieldInfo fi= typeof(A).GetField("field1");
if (fi != null)
{
fi.SetValue(null, "456");
}
Console.WriteLine(A.field1);
Console.ReadKey();
}
class A
{
public static readonly string field1 = "123";
}
}
}
输出:
123
456
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;
}
const和static readonly很像:通过类名而不是对象名进行访问,在程序中只读等等。在多数情况下可以混用。二者本质的区别在于:
* const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值。
* static readonly是在运行时计算出其值的, 只有两种途径为其赋值,在声明该变量的时候或在默认的静态构造函数里面为其赋值。实际上这两种方法最后生成的IL代码是相同的(都是在静态构造函数中赋值)。
明白了这个本质区别,我们就不难看出下面的语句中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,那么后面的两个语句就都会出错。
static readonly的field可以被反射修改。
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(A.field1);
FieldInfo fi= typeof(A).GetField("field1");
if (fi != null)
{
fi.SetValue(null, "456");
}
Console.WriteLine(A.field1);
Console.ReadKey();
}
class A
{
public static readonly string field1 = "123";
}
}
}
输出:
123
456
相关文章推荐
- static、const、readonly与static readonly的区别与联系
- C# FAQ: const和static readonly有什么区别?
- c# 中const 和 static readonly 的区别
- const和static readonly的区别
- C#中const与 static readonly 区别(转)
- const 和 static readonly 区别
- C#中const与static readonly的区别
- What is the difference between const and static readonly?(const 和 static readonly 的区别?)
- C# static readonly 与 const 的区别
- C#老生常谈之const与static readonly只区别
- const和static readonly的区别
- ASP.NET 中 const 与 static readonly 的区别
- C# FAQ: const和static readonly有什么区别?
- C#中的const与static readonly区别
- .net程序员的盲点(三):const和static readonly的区别
- .net程序员的盲点(三):const和static readonly的区别
- C# FAQ: const和static readonly有什么区别?
- C#中Const与Static Readonly的区别
- const,readonly和static readonly的区别
- .net程序员的盲点(三):const和static readonly的区别