前段时间在CSDN看到有个帖子问关于常量(const )和只读(readonly)的区别,觉得很有意思,于是找了些资料,写了些代码研究了一下。
发现常量和只读变量有以下区别
1.常量必须在声明时就被初始化,指定了值后就不能修改了.只读字段可以在声明时被初始化,也可以在构造函数中指定初始化的值,在构造以后值就不能修改.
2.常量是静态的,而自读字段可以是静态和动态的
3.Const可以用在字段和局部变量,readonly只可以修饰字段
以下是我的测试代码
using
System;
![None.gif](/Images/OutliningIndicators/None.gif)
//
说明:没有编号的字段在声明时不初始化,构造时被初始化
//
编号为1的字段在声明时初始化,构造时不被初始化
//
编号为2的字段在声明时,构造时都被初始化
//
编号为3的字段在声明时,构造时都不被初始化
public
class
Test
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
...
{
//常量
//常量必须在声明时就被初始化,使用下面语句的出现编译错误"常数字段要求提供一个值"
//const string myConst;
const string myConst="声明时被初始化";
//静态只读
public static readonly string myStaticReadonly;
public static readonly string myStaticReadonly1="声明时被初始化";
public static readonly string myStaticReadonly2="声明时被初始化";
public static readonly string myStaticReadonly3;
//只读
public readonly string myReadonly;
public readonly string myReadonly1="声明时被初始化";
public readonly string myReadonly2="声明时被初始化";
public readonly string myReadonly3;
//默认构造函数
public Test()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
myReadonly="默认构造函数时被初始化";
myReadonly2="默认构造函数时被初始化";
}
//带参构造函数
public Test(string arr)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
myReadonly=arr;
myReadonly2=arr;
}
//静态构造函数,此函数不能带参数
static Test()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
myStaticReadonly="构造时被初始化";
myStaticReadonly2="构造时被初始化";
//常量的值不能在构造函数中被更改,使用下面语句的出现编译错误"赋值号左边必须是变量、属性或索引器"
//myConst="value";
}
//试验在用静态方法改变常量和自读变量的值
public static void StaticChangeValue()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//常量的值不能被改变,使用下面语句的出现编译错误"赋值号左边必须是变量、属性或索引器"
//myConst="被改变";
//无法在构造后改变自读字段的值,使用下面语句的出现编译错误"无法对静态只读字段赋值(静态构造函数或变量初始值设定项中除外)"
//myStaticReadonly="被改变";
}
//试验在用方法改变自读变量的值
public void ChangeValue()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//无法在构造后改变自读字段的值,使用下面语句的出现编译错误"无法对只读的字段赋值(构造函数或变量初始值指定项中除外)"
//myReadonly="被改变";
}
//试验静态局部常量和自读变量
public static void Include()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
const string includeConst="局部静态常量";
//readonly不能修饰静态局部变量
//static readonly includeReadonly="局部静态自读变量";
}
//试验局部自读变量
public void StaticInclude()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//readonly不能修饰局部变量
//readonly staticIncludeReadonly="局部静态自读变量";
}
public static void Main()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
//参看静态数据
Console.WriteLine("Test.myConst:"+Test.myConst);
Console.WriteLine("Test.myStaticReadonly:"+Test.myStaticReadonly);
Console.WriteLine("Test.myStaticReadonly1:"+Test.myStaticReadonly1);
Console.WriteLine("Test.myStaticReadonly2:"+Test.myStaticReadonly2);
Console.WriteLine("Test.myStaticReadonly3:"+Test.myStaticReadonly3);
Console.WriteLine("");
Test test=new Test();
Console.WriteLine("test.myReadonly:"+test.myReadonly);
Console.WriteLine("test.myReadonly1:"+test.myReadonly1);
Console.WriteLine("test.myReadonly2"+test.myReadonly2);
Console.WriteLine("test.myReadonly3"+test.myReadonly3);
Console.WriteLine("");
Test test1=new Test("带参构造函数时被初始化");
Console.WriteLine("test1.myReadonly:"+test1.myReadonly);
Console.WriteLine("test1.myReadonly1:"+test1.myReadonly1);
Console.WriteLine("test1.myReadonly2"+test1.myReadonly2);
Console.WriteLine("test1.myReadonly3"+test1.myReadonly3);
Console.WriteLine("");
Console.WriteLine("从结果可以看出构造时初始化的值将覆盖声明时初始化的值");
Console.WriteLine("没有初始化的只读字段被赋予默认值,空值");
Console.Read();
}
}