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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#基礎(chǔ)概念學(xué)習(xí)總結(jié)

C#基礎(chǔ)概念之一,靜態(tài)變量和非靜態(tài)變量的區(qū)別?

泰寧ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

靜態(tài)變量:
靜態(tài)變量使用 static 修飾符進(jìn)行聲明,在所屬類被裝載時創(chuàng)建,通過類進(jìn)行訪問,所屬類的所有實例的同一靜態(tài)變量都是同一個值

非靜態(tài)變量:
不帶有 static 修飾符聲明的變量稱做非靜態(tài)變量,在類被實例化時創(chuàng)建,通過對象進(jìn)行訪問,同一個類的不同實例的同一非靜態(tài)變量可以是不同的值

示例:

 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Example01 { 
  5. class Program { 
  6. class Class1 { 
  7. public static String staticStr = "Class";public String notstaticStr = "Obj";
  8. static void Main(string[] args)
  9. //靜態(tài)變量通過類進(jìn)行訪問,該類所有實例的同一靜態(tài)變量都是同一個值
  10. Console.WriteLine("Class1's staticStr: {0}", Class1.staticStr);
  11. Class1 tmpObj1 = new Class1();
  12. tmpObj1.notstaticStr = "tmpObj1";
  13. Class1 tmpObj2 = new Class1();
  14. tmpObj2.notstaticStr = "tmpObj2";
  15. //非靜態(tài)變量通過對象進(jìn)行訪問,不同對象的同一非靜態(tài)變量可以有不同的值
  16. Console.WriteLine("tmpObj1's notstaticStr: {0}", tmpObj1.notstaticStr);
  17. Console.WriteLine("tmpObj2's notstaticStr: {0}", tmpObj2.notstaticStr);
  18. Console.ReadLine();
  19. }

C#基礎(chǔ)概念之二,const 和 static readonly 區(qū)別?

const
用 const 修飾符聲明的成員叫常量,是在編譯期初始化并嵌入到客戶端程序

static readonly
用 static readonly 修飾符聲明的成員依然是變量,只不過具有和常量類似的使用方法:通過類進(jìn)行訪問、初始化后不可以修改。但與常量不同的是這種變量是在運行期初始化

示例:

測試類:

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

客戶端代碼:

 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Example02Lib;
  5. namespace Example02 { 
  6. class Program { 
  7. static void Main(string[] args)
  8. //修改Example02中Class1的strConst初始值后,只編譯Example02Lib項目
  9. //然后到資源管理器里把新編譯的 Example02Lib.dll拷貝Example02.exe所在的目錄,
    執(zhí)行Example02.exe 
  10. //切不可在IDE里直接調(diào)試運行因為這會重新編譯整個解決方案?。?
  11. //可以看到strConst的輸出沒有改變,而strStaticReadonly的輸出已經(jīng)改變/
  12. /表明Const變量是在編譯期初始化并嵌入到客戶端程序,而StaticReadonly是在運行時初始化的
  13. Console.WriteLine("strConst : {0}", Class1.strConst);
  14. Console.WriteLine("strStaticReadonly : {0}", Class1.strStaticReadonly);
  15. Console.ReadLine();
  16. }

修改后的示例:

測試類:

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

以上介紹兩點C#基礎(chǔ)概念


當(dāng)前題目:C#基礎(chǔ)概念學(xué)習(xí)總結(jié)
網(wǎng)頁網(wǎng)址:http://www.5511xx.com/article/dpgishh.html