const 变量的值在编译时确定。
static readonly 变量的值在运行时确定。
static readonly 变量可以在定义时和类型构造函数中初始化。
const 值类型只能在定义时被初始化,且引用类型只能被初始化为null.
一个简单的示例:
public class Person
{
}
public class Program
{
//private const Person p1 = new Person(); Error
private const Person p1 = null;
private static readonly Person p2 = new Person();
static Program()
{
//p1 = new Person(); Error
p2 = new Person();
}
public Program()
{
//p1 = new Person(); Error
//p2 = new Person(); Error
}
}