日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C# const和static readonly區(qū)別淺談

C#語言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C# const和static readonly區(qū)別,包括介紹用const修飾符聲明的成員叫常量和static readonly修飾符聲明的成員依然是變量等方面。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、太子河網(wǎng)站維護(hù)、網(wǎng)站推廣。

C# const和static readonly區(qū)別

const: 用const修飾符聲明的成員叫常量,是在編譯期初始化并嵌入到客戶端程序
static readonly: 用static readonly修飾符聲明的成員依然是變量,只不過具有和常量類似的使用方法:通過類進(jìn)行訪問、初始化后不可以修改。但與常量不同的是這種變量是在運(yùn)行期初始化。

C# const和static readonly區(qū)別示例:

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.    
  5. namespace Example02Lib  
  6. {  
  7. public class Class1  
  8. {  
  9. public const String strConst = "Const";  
  10. public static readonly String strStaticReadonly = "StaticReadonly";  
  11. //public const String strConst = "Const Changed";  
  12. //public static readonly String strStaticReadonly = "StaticReadonly Changed";  
  13. }  

客戶端代碼:

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using Example02Lib;   
  5. namespace Example02  
  6. {  
  7. class Program  
  8. {  
  9. static void Main(string[] args)  
  10. {  
  11. //修改Example02中Class1的strConst初始值后,只編譯Example02Lib項(xiàng)目  
  12. //然后到資源管理器里把新編譯的Example02Lib.dll拷貝Example02.exe所在的目錄,

    執(zhí)行Example02.exe  
  13. //切不可在IDE里直接調(diào)試運(yùn)行因?yàn)檫@會(huì)重新編譯整個(gè)解決方案?。?nbsp;  
  14. //可以看到strConst的輸出沒有改變,而strStaticReadonly的輸出已經(jīng)改變  
  15. //表明Const變量是在編譯期初始化并嵌入到客戶端程序,而StaticReadonly是在運(yùn)行時(shí)初始化的  
  16. Console.WriteLine("strConst : {0}", Class1.strConst);  
  17. Console.WriteLine("strStaticReadonly : {0}", Class1.strStaticReadonly);   
  18. Console.ReadLine();  
  19. }  
  20. }  

修改后的示例:

 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.    
  5. namespace Example02Lib  
  6. {  
  7. public class Class1  
  8. {  
  9. //public const String strConst = "Const";  
  10. //public static readonly String strStaticReadonly = "StaticReadonly";  
  11. public const String strConst = "Const Changed";  
  12. public static readonly String strStaticReadonly = "StaticReadonly Changed";  
  13. }  

【編輯推薦】

  1. C# const常量詳細(xì)介紹
  2. C# Lambda表達(dá)式學(xué)習(xí)筆記
  3. C#隱式類型局部變量經(jīng)驗(yàn)總結(jié)
  4. 調(diào)用C# Thread.Start()方法
  5. C# CheckStatus()方法

網(wǎng)站題目:C# const和static readonly區(qū)別淺談
本文地址:http://www.5511xx.com/article/cdojddc.html