码迷,mamicode.com
首页 > 其他好文 > 详细

const和readonly

时间:2015-10-28 22:58:52      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

const:常量,编译时即需要确定值

readonly:只读变量,运行时确定值

技术分享
 1     class ConstReadonlyTest
 2     {
 3         //const String str;//错误:常量字段定义时需要指定初始值
 4         //const Object obj = new Object();//错误:常量字段不能使用new初始化,表达式必须是常量(字符串可以)
 5         //const Object obj = new StructTest();//错误:表达式必须是常量不能使用new初始化(即使是值类型也不可以)
 6 
 7         public readonly string familyname = "";//正确:readonly可以修饰全局变量
 8         public void Funk()
 9         {
10             //readonly string name = "tom";//错误:局部变量不能使用readonly修饰
11             const int age = 12;//正确:const可以修饰局部变量
12         }
13     }
14     struct StructTest
15     {
16     }
View Code

 · const、readonly和static readonly定义的常量指定初始值后(包括在构造函数中初始化)将不可更改

· const必须在声明时指定初始值;readonly和staticreadonly可以在声明时初始化也可以在构造函数中初始化,在构造函数中初始化的值会覆盖直接初始化的值

· static readonly常量如果要在构造函数中初始化也只能在静态构造函数中初始化(因为static字段需要在构造函数初始化就只能在静态构造函数中进行)

· const可以定义局部常量和字段常量;readonly和static readonly不能定义局部常量

技术分享
 1  private const String Name = "Tom";
 2         private readonly Int32 Age = 18;
 3         private readonly String Sex;
 4         private static readonly String Title = "工程师";
 5 
 6         public CoonstReadonly()
 7         {
 8             Age = 19;
 9         }
10         public CoonstReadonly(Int32 age, string sex)
11         {
12             this.Age = age;
13             this.Sex = sex;
14         }
15         static CoonstReadonly()
16         {
17             Title = "高级工程师";
18         }
19 
20         public void Init()
21         {
22             const string Des = "局部变量";
23             Console.WriteLine(Des);
24         }
25         static void Main(string[] args)
26         {
27             Console.WriteLine(CoonstReadonly.Name);
28             Console.WriteLine(CoonstReadonly.Title);
29 
30             CoonstReadonly cd = new CoonstReadonly();
31             Console.WriteLine(cd.Age);
32             Console.WriteLine(cd.Sex);
33             cd.Init();
34 
35         }
View Code

const可能存在的不一致问题

 引用程序集时const常量将直接被编译到引用程序集中;readonly则在动态调用时才获取真实值。因此跨程序集应用的系统中要注意避免const常量带来的不一致问题

首先我们创建一个类库项目MyConst,添加一个类然后编译通过

技术分享
1     public class MyConst
2     {
3         public const String ConstString = "ConstString";
4         public static readonly String StatcReadonlyString = "StatcReadonlyString";
5     }
View Code

新建控制台项目CoonstReadonly引用类库项目生成的dll

技术分享
1         static void Main(string[] args)
2         {
3             Console.WriteLine(MyConst.MyConst.ConstString);
4             Console.WriteLine(MyConst.MyConst.StatcReadonlyString);
5             Console.ReadLine();
6         }
View Code

执行结果为:ConstString StatcReadonlyString

此时修改MyConst类库项目

技术分享
1     public class MyConst
2     {
3         public const String ConstString = "NewConstString";
4         public static readonly String StatcReadonlyString = "NewStatcReadonlyString";
5     }
View Code

将生成的MyConstdll直接放到CoonstReadonly控制台项目的bin目录下直接执行CoonstReadonly.exe程序(不要重新编译CoonstReadonly项目)

此时执行结果为:ConstString NewStatcReadonlyString;Const常量由于项目未重新编译引发了前后不一致的问题

const是编译时常量,readonly是运行时常量;应用中推荐使用static readonly代替const,可以避免程序不一致问题而且可以实现延迟初始化

const和readonly

标签:

原文地址:http://www.cnblogs.com/registed/p/4918692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!