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(2007,7,1,1,1,1);
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
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在编译时就确定了。
- C#中的const与static readonly区别
- C#中const与 static readonly 区别
- C#中const与 static readonly 区别
- C# const,readonly,static区别
- C#中的readonly与const 的区别
- C#中的readonly与const区别
- C#中的readonly与const区别
- C#老生常谈之const与static readonly只区别
- C#中const与static readonly的区别
- C#中Const与Static Readonly的区别
- c# const 与 static readonly 在switch中的注意事项
- C#中的static、readonly与const的比较
- C#中的static、readonly与const的比较
- C#中的static、readonly与const的比较
- C#中的static、readonly与const的比较
- C# static const和readonly区别
- C# const ,readonly,static之间的区别
- C# const和static readonly区别
- Eclipse 运行命令行参数大全
- IEEE Computer Society数据库介绍2009
- SQL Server 数据库优化方法 (一)
- DA9034驱动程序阅读笔记(3)
- C语言库函数strstr的实现
- C#中的const与static readonly区别
- GCC参数一览
- 一个简单的osworkflow的流程配置文件
- Net线程间通信的异步机制
- session与cookie的区别
- iis提示“另一个程序正在使用此文件,进程无法访问”解决办法
- 山重水复疑无路 柳暗花明又一村
- Quartz 使用手记
- TOMCAT-5.5.X优化配置