您好,登錄后才能下訂單哦!
關(guān)鍵字 | const | readonly |
名稱 |
靜態(tài)常量 編譯時(shí)常量(compile-time constants) |
動(dòng)態(tài)常量 運(yùn)行時(shí)常量(runtime constants) |
初始化、賦值時(shí)機(jī) |
const 字段只能在該字段的聲明中初始化。 |
readonly 字段可以在聲明或構(gòu)造函數(shù)中初始化。 |
工作原理 | const為編譯時(shí)常量,程序編譯時(shí)將所有常量引用替換為相應(yīng)值。 | readonly為運(yùn)行時(shí)常量,程序運(yùn)行時(shí)賦值,賦值完成后便無法更改 |
可維護(hù)性 | const是編譯時(shí)替換,防止跨程序集調(diào)用問題,所有相關(guān)程序集都得編譯。 | readonly常量更新后,所有引用該常量的地方均能得到更新。 |
使用場景 | 取值恒定不變,范圍較小。對程序性能要求較高 |
取值類型范圍較大,可以是運(yùn)行時(shí)的常量。如: public readonly DateTime D = DateTime.MinValue; 性能略有損耗。 |
初始化、賦值時(shí)機(jī) 示例說明:
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;//無法使用實(shí)例引用來訪問成員
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;//無法對只讀的字段賦值(構(gòu)造函數(shù)或變量初始值指定項(xiàng)中除外)
18: }
19:
20: static void Main(string[] args)
21: {
22: Class1 instance1 = new Class1(2013);
23: //instance1._x = 2014;//無法對只讀的字段賦值(構(gòu)造函數(shù)或變量初始值指定項(xiàng)中除外)
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: }
運(yùn)行結(jié)果:
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: }
運(yùn)行結(jié)果:
分析兩種結(jié)果:
1) 啟動(dòng)“ILDASM”
2) 對比兩種結(jié)果
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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。