您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“C#靜態(tài)變量和非靜態(tài)變量的區(qū)別”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
C#基礎(chǔ)概念之一,靜態(tài)變量和非靜態(tài)變量的區(qū)別?
靜態(tài)變量:
靜態(tài)變量使用 static 修飾符進(jìn)行聲明,在所屬類(lèi)被裝載時(shí)創(chuàng)建,通過(guò)類(lèi)進(jìn)行訪問(wèn),所屬類(lèi)的所有實(shí)例的同一靜態(tài)變量都是同一個(gè)值
非靜態(tài)變量:
不帶有 static 修飾符聲明的變量稱(chēng)做非靜態(tài)變量,在類(lèi)被實(shí)例化時(shí)創(chuàng)建,通過(guò)對(duì)象進(jìn)行訪問(wèn),同一個(gè)類(lèi)的不同實(shí)例的同一非靜態(tài)變量可以是不同的值
示例:
using System; using System.Collections.Generic; using System.Text; namespace Example01 { class Program { class Class1 { public static String staticStr = "Class";public String notstaticStr = "Obj"; } static void Main(string[] args) { //靜態(tài)變量通過(guò)類(lèi)進(jìn)行訪問(wèn),該類(lèi)所有實(shí)例的同一靜態(tài)變量都是同一個(gè)值 Console.WriteLine("Class1's staticStr: {0}", Class1.staticStr); Class1 tmpObj1 = new Class1(); tmpObj1.notstaticStr = "tmpObj1"; Class1 tmpObj2 = new Class1(); tmpObj2.notstaticStr = "tmpObj2"; //非靜態(tài)變量通過(guò)對(duì)象進(jìn)行訪問(wèn),不同對(duì)象的同一非靜態(tài)變量可以有不同的值 Console.WriteLine("tmpObj1's notstaticStr: {0}", tmpObj1.notstaticStr); Console.WriteLine("tmpObj2's notstaticStr: {0}", tmpObj2.notstaticStr); Console.ReadLine(); }
C#基礎(chǔ)概念之二,const 和 static readonly 區(qū)別?
const
用 const 修飾符聲明的成員叫常量,是在編譯期初始化并嵌入到客戶(hù)端程序
static readonly
用 static readonly 修飾符聲明的成員依然是變量,只不過(guò)具有和常量類(lèi)似的使用方法:通過(guò)類(lèi)進(jìn)行訪問(wèn)、初始化后不可以修改。但與常量不同的是這種變量是在運(yùn)行期初始化
示例:
測(cè)試類(lèi):
using System; using System.Collections.Generic; using System.Text; namespace Example02Lib { public class Class1 { public const String strConst = "Const";public static readonly String strStaticReadonly = "StaticReadonly"; public const String strConst = "Const Changed"; public static readonly String strStaticReadonly = "StaticReadonly Changed"; }
客戶(hù)端代碼:
using System;
using System.Collections.Generic;
using System.Text;
using Example02Lib;
namespace Example02 {
class Program {
static void Main(string[] args)
{
//修改Example02中Class1的strConst初始值后,只編譯Example02Lib項(xiàng)目
//然后到資源管理器里把新編譯的 Example02Lib.dll拷貝Example02.exe所在的目錄,
執(zhí)行Example02.exe//切不可在IDE里直接調(diào)試運(yùn)行因?yàn)檫@會(huì)重新編譯整個(gè)解決方案!!
//可以看到strConst的輸出沒(méi)有改變,而strStaticReadonly的輸出已經(jīng)改變/
/表明Const變量是在編譯期初始化并嵌入到客戶(hù)端程序,而StaticReadonly是在運(yùn)行時(shí)初始化的
Console.WriteLine("strConst : {0}", Class1.strConst);
Console.WriteLine("strStaticReadonly : {0}", Class1.strStaticReadonly);
Console.ReadLine();
}
修改后的示例:
測(cè)試類(lèi):
using System; using System.Collections.Generic; using System.Text; namespace Example02Lib { public class Class1 { //public const String strConst = "Const"; //public static readonly String strStaticReadonly = "StaticReadonly"; public const String strConst = "Const Changed"; public static readonly String strStaticReadonly = "StaticReadonly Changed"; }
“C#靜態(tài)變量和非靜態(tài)變量的區(qū)別”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。