您好,登录后才能下订单哦!
| 关键字 | 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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。