1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.Threading;
5![](/Images/OutliningIndicators/None.gif)
6
namespace
CSharpFoundationStudy
7![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
8![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
9
* const与readonly, static readonly的区别
10
* const修饰符定义常量,在定义常量时必须指定初始值,而且不能更改
11
* readonly修饰符用于"只读域",可以在动态运行时指定,但指定后也不能改变
12
* static readonly 只能在声明时初始化或者静态构造函数中初始化
13
* const或者static readonly修饰的常量是属于类级别的;而readonly修饰的,无论是直接通过赋值来初始化或者在实例构造函数里初始化,都属于实例对象级别
14
*
15
* 详细内容参见: C#学习simply-zhao\readonly vs const.doc
16
*/
17
public class ConstReadOnly
18![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
const string Constant = "Constant, Can't be changed";
20
public readonly string ReadOnly;
21
static readonly string SReadOnly = "Static ReadOnly";
22![](/Images/OutliningIndicators/InBlock.gif)
23
//静态构造函数
24
static ConstReadOnly()
25![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
//ConstReadOnly.Constant = "Change Constant Failed"; //Error 常量不能更改
27
ConstReadOnly.SReadOnly = "Change Static ReadOnly in Static Constructor Successfully";
28
}
29![](/Images/OutliningIndicators/InBlock.gif)
30
public ConstReadOnly()
31![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
ReadOnly = DateTime.Now.ToString();
33
}
34![](/Images/OutliningIndicators/InBlock.gif)
35
public override string ToString()
36![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
return ConstReadOnly.Constant + "\n" + this.ReadOnly + "\n" + ConstReadOnly.SReadOnly + "\n";
38
}
39![](/Images/OutliningIndicators/InBlock.gif)
40
//public static void Main()
41
//{
42
// ConstReadOnly instance1 = new ConstReadOnly();
43
// Console.WriteLine(instance1);
44
// Thread.Sleep(2000);
45
// ConstReadOnly instance2 = new ConstReadOnly();
46
// Console.WriteLine(instance2);
47
// Console.ReadLine();
48
//}
49
}
50
}
51
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
50
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
51
![](/Images/OutliningIndicators/None.gif)