C#中的const与static readonly区别

来源:互联网 发布:pk计划软件 编辑:程序博客网 时间:2024/05/17 07:43

在许多教程中常看到列出了两者之间的区别
  const 
       (1)在编译期间解析常量
       (2)必须在申明时就初始化
       (3)即可用来修饰类中的成员,也可用来修饰函数中的局部变量
  static readonly
      (1)在运行期间解析常量
      (2)即可在申明时初始化,也可在构造器中初始化
      (3)只可用来修饰类中的成员

 

从上面的区别看似乎也很明白,可是在实际的使用中还是不太好把握。在c#中有两种不同的常量:
静态常量(compile-time constants) 和动态常量(runtime constants),他们之间有不同的特性,错误的使用不仅会损失效率,
还可能造成错误。相比之下,静态常量在速度上会稍快写,但是灵活性却比动态常量差。

 从我个人对他们的使用中总结出有如下三点需要注意:
(1)当使用new来初始化一个常量,不管他是值类型,还是引用类型都只能使用static readonly


1static readonly MyClass myclass=new MyClass();
2//这样是错误的
3public const DateTime myDateTime=new DateTime(2007,7,1,1,1,1);
4//这样是正确的
5public static readonly DateTime myDateTime=new DateTime(200771111);
6

 


(2) 比较编译时和运行时他们之间的区别

1static readonly A=B*20
2static readonly B=10
3//在编译的时候B并没有被编译为10,A也没有被编译为200
4const A=B*20
5const B=10
6//显然在编译的时候,就把B编译为10,A编译为200了,而不是在运行时计算B*20


(3)在不同的项目中使用const和static readonly的区别


 1//下面有一个项目
 2public class OneClass
 3{
 4 public const int myInt=5;
 5 public static readonly string strStaticReadonly="StaticReadonly";
 6}

 7//在另一个项目中引用了上述属性
 8public class AnotherClass
 9{
10int i=OneClass.myInt;
11string ss=OneClass.strStaticReadonly;
12}

13//编译时运行程,i=5;ss="StaticReadonly",这是理所当然的,但是我们如过了OneClass中的值
14public class OneClass
15{
16 public const int myInt=10;
17 public static readonly string strStaticReadonly="Changed";
18}

19//然后编译OneClass项目,生成DLL,运行AnotherClass(只是运行,并不是编译后运行,发现结果是:
20//i=5不变,但是ss=“changed”,为什么会这样,原因是在AnotherClass中,i已经被定义为5了,而不是运行时再去取dll的值,所以说const在编译时就确定了。
原创粉丝点击