新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#靜態(tài)變量簡單分析
C#語言還是比較常見的東西,這里我們主要介紹C#靜態(tài)變量,包括介紹C#靜態(tài)變量使用 static 修飾符進(jìn)行聲明等方面。

C#靜態(tài)變量使用 static 修飾符進(jìn)行聲明,在類被實例化時創(chuàng)建,通過類進(jìn)行訪問不帶有 static 修飾符聲明的變量稱做非靜態(tài)變量,在對象被實例化時創(chuàng)建,通過對象進(jìn)行訪問一個類的所有實例的同一C#靜態(tài)變量都是同一個值,同一個類的不同實例的同一非靜態(tài)變量可以是不同的值。靜態(tài)函數(shù)的實現(xiàn)里不能使用非靜態(tài)成員,如非靜態(tài)變量、非靜態(tài)函數(shù)等。
- 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)變量通過類進(jìn)行訪問,該類所有實例的同一靜態(tài)變量都是同一個值
- Console.WriteLine("Class1's staticStr: {0}", Class1.staticStr);
- Class1 tmpObj1 = new Class1();
- tmpObj1.notstaticStr = "tmpObj1";
- Class1 tmpObj2 = new Class1();
- tmpObj2.notstaticStr = "tmpObj2";
- //非靜態(tài)變量通過對象進(jìn)行訪問,不同對象的同一非靜態(tài)變量可以有不同的值
- Console.WriteLine("tmpObj1's notstaticStr: {0}", tmpObj1.notstaticStr);
- Console.WriteLine("tmpObj2's notstaticStr: {0}", tmpObj2.notstaticStr);
- Console.ReadLine();
- }
- }
- }
分享文章:C#靜態(tài)變量簡單分析
鏈接地址:http://www.fisionsoft.com.cn/article/copccip.html


咨詢
建站咨詢
