您好,登錄后才能下訂單哦!
關鍵字 | 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。