精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網站建設資訊

NEWS

網站建設資訊

C#語言知識點整理-常量

關鍵字 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: }

運行結果:

C#語言知識點整理 - 常量

 

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: }

 

 

運行結果:

C#語言知識點整理 - 常量

 

分析兩種結果:

1) 啟動“ILDASM”

C#語言知識點整理 - 常量

2) 對比兩種結果

ConstantTest:

C#語言知識點整理 - 常量

.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:

C#語言知識點整理 - 常量

.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


新聞名稱:C#語言知識點整理-常量
瀏覽地址:http://m.jcarcd.cn/article/jpihch.html
主站蜘蛛池模板: 欧美在线色 | 国产精品女a | 日韩视频第1 | 精品国产午夜大片 | 国产精品第157页 | 国产亚洲制服免视频 | 欧美最新免费一区 | 国产欧美乱伦中文 | 精品成年 | 欧美性爱福| 福利影院在线看 | 国产精品一卡 | 精品在线热 | 91大片| 欧洲精品 | 国产在线视频 | 精品性高 | 国产精品丝袜高跟鞋 | 伦理一国产A级 | 国产不卡福利片在 | 国产精品无 | 国产主播一区 | 欧洲成人动漫在线观 | 国产永久精品91在 | 91免费观看 | 91神马高 | 福利导航在线视频 | 99电影网 | 成人国内 | 人伦小说视频在线 | 精品一区精品二区 | 国产欧美日韩在线视 | 97视频网站 | 国产精品无需播放器 | 日韩午夜福利院 | 国产色秀精品综合 | 成人影视免 | 国产妇人成 | 日韩在线观看精品 | 国产资源免费观看 | 青草影视 |