重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
关键字 | const | readonly |
名称 | 静态常量 创新互联是一家集网站建设,泰顺企业网站建设,泰顺品牌网站建设,网站定制,泰顺网站建设报价,网络营销,网络优化,泰顺网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。 编译时常量(compile-time constants) | 动态常量 运行时常量(runtime constants) |
初始化、赋值时机 | const 字段只能在该字段的声明中初始化。 | readonly 字段可以在声明或构造函数中初始化。 |
工作原理 | const为编译时常量,程序编译时将所有常量引用替换为相应值。 | readonly为运行时常量,程序运行时赋值,赋值完成后便无法更改 |
可维护性 | const是编译时替换,防止跨程序集调用问题,所有相关程序集都得编译。 | readonly常量更新后,所有引用该常量的地方均能得到更新。 |
使用场景 | 取值恒定不变,范围较小。对程序性能要求较高 | 取值类型范围较大,可以是运行时的常量。如: public readonly DateTime D = DateTime.MinValue; 性能略有损耗。 |
初始化、赋值时机 示例说明:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace CSharp.Contant_const
7: {
8: class Class1
9: {
10: const int _x=2013;
11:
12: Class1(int x)
13: {
14: //_x = x;//赋值号左边必须是变量、属性或索引器
15: }
16:
17: void Method()
18: {
19: //_x = 2013;//赋值号左边必须是变量、属性或索引器
20: }
21:
22: static void Main(string[] args)
23: {
24: //Class1 instance1 = new Class1(2013);
25: //instance1._x = 2014;//无法使用实例引用来访问成员
26:
27: //Class1._x = 2014;//赋值号左边必须是变量、属性或索引器
28:
29: Console.WriteLine("x:{0}",Class1._x);
30: }
31: }
32: }
33:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace CSharp.Constant_readonly
7: {
8: class Class1
9: {
10: readonly int _x;
11: Class1(int x)
12: {
13: _x = x;
14: }
15: void Method()
16: {
17: //_x = 2013;//无法对只读的字段赋值(构造函数或变量初始值指定项中除外)
18: }
19:
20: static void Main(string[] args)
21: {
22: Class1 instance1 = new Class1(2013);
23: //instance1._x = 2014;//无法对只读的字段赋值(构造函数或变量初始值指定项中除外)
24: }
25: }
26: }
27:
工作原理示例说明:
ConstTest:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace CSharp.Contant_const
7: {
8: class ConstTest
9: {
10: private const int b = a * 3;
11: private const int a = 2;
12: static void Main(string[] args)
13: {
14: Console.WriteLine("a={0}\nb={1}",a,b);
15: }
16: }
17: }
运行结果:
ReadonlyTest:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace CSharp.Constant_readonly
7: {
8: class ReadonlyTest
9: {
10: private static readonly int b = a * 3;
11: private static readonly int a = 2;
12: static void Main(string[] args)
13: {
14: Console.WriteLine("a={0}\nb={1}", a, b);
15: }
16: }
17: }
运行结果:
分析两种结果:
1) 启动“ILDASM”
2) 对比两种结果
ConstantTest:
.field private static literal int32 a = int32(0x00000002)
.field private static literal int32 b = int32(0x00000006)
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 25 (0x19)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "a={0}\nb={1}"
IL_0006: ldc.i4.2
IL_0007: box [mscorlib]System.Int32
IL_000c: ldc.i4.6
IL_000d: box [mscorlib]System.Int32
IL_0012: call void [mscorlib]System.Console::WriteLine(string,
object,
object)
IL_0017: nop
IL_0018: ret
} // end of method ConstTest::Main
ReadonlyTest:
.field private static initonly int32 a
.field private static initonly int32 b
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 33 (0x21)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "a={0}\nb={1}"
IL_0006: ldsfld int32 CSharp.Constant_readonly.ReadonlyTest::a
IL_000b: box [mscorlib]System.Int32
IL_0010: ldsfld int32 CSharp.Constant_readonly.ReadonlyTest::b
IL_0015: box [mscorlib]System.Int32
IL_001a: call void [mscorlib]System.Console::WriteLine(string,
object,
object)
IL_001f: nop
IL_0020: ret
} // end of method ReadonlyTest::Main