一、變量
C#共有其中變量類型有:靜態(tài)變量、實(shí)類變量、數(shù)組元素、數(shù)值參數(shù)、引用參數(shù)、輸出參數(shù)和局部變量
先定義一個(gè)簡單的類來說明,如下:
public class VariableDefine  {    private static uint variableUInt;    public static uint VariableUInt { get => variableUInt; set => variableUInt = value; }    string VariableStr;    public VariableDefine(string version)    {      VariableStr = version;    }    public static void Fun()    {        Console.WriteLine(variableUInt);    }    /// <summary>    /// 變量類型    /// </summary>    /// <param name="intArray">intArray[0]數(shù)組元素</param>    /// <param name="a">數(shù)值參數(shù)</param>    /// <param name="b">引用類型</param>    /// <param name="c">輸出參數(shù)</param>    public void Fun(int[] intArray, int a, ref int b, out int c)    {      //局部變量i      var i = 0;      c = i;    }  }對(duì)于靜態(tài)變量在被創(chuàng)建加載之后失效,當(dāng)被卸載后失效,靜態(tài)變量的初始值也為此類型的變量的默認(rèn)值
對(duì)于實(shí)例變量當(dāng)創(chuàng)建某類的一個(gè)實(shí)例的時(shí)候,隸屬于該類的實(shí)例變量也被生成,當(dāng)不再有關(guān)于這個(gè)實(shí)例的引用而且實(shí)例的析構(gòu)函數(shù)執(zhí)行了以后,此實(shí)例變量失效
對(duì)于數(shù)組元素當(dāng)任意一個(gè)數(shù)組實(shí)例被創(chuàng)建時(shí),這個(gè)數(shù)組的元素也被同時(shí)創(chuàng)建,當(dāng)不再有任何正對(duì)這個(gè)數(shù)組實(shí)例的引用時(shí),它的元素也就此失效
當(dāng)一個(gè)不帶有ref 或out 修飾參數(shù)被聲明時(shí),我們稱它為數(shù)值參數(shù)
參數(shù)當(dāng)一個(gè)帶有ref 修飾語的參數(shù)被聲明時(shí),我們稱之為引用參數(shù)
參數(shù)當(dāng)一個(gè)帶有out 修飾語的參數(shù)被聲明時(shí),我們稱之為輸出參數(shù)
局部變量被局部變量聲明語句創(chuàng)建
C#編譯器不容許在表達(dá)式中使用未初始化的變量
變量初始化要注意兩點(diǎn)的是:(1)變量是類或結(jié)構(gòu)中的字段,如果沒有顯示初始化,創(chuàng)建這些變量時(shí),其默認(rèn)值就是0(2)方法的局部變量必須在代碼中顯示初始化,之后才能在語句中使用它們的值。
二、常量
C#可以定義兩種類型的常量,靜態(tài)常量用const來定義在程序編譯的時(shí)候確定,一種是動(dòng)態(tài)常量用readonly來定義在運(yùn)行時(shí)確定
靜態(tài)常量使用方便,性能高,但一旦定義之后就不可以改變,在一個(gè)引用第三方程序集上面如果定義了一個(gè)靜態(tài)常量,當(dāng)它定義的值改變時(shí)你不得不重新引用生成主程序
動(dòng)態(tài)常量使用靈活,能很好的支持程序的擴(kuò)展性
下面一個(gè)事例就是通過讀取XML文檔來給動(dòng)態(tài)常量賦值
public sealed class ReadOnlyModel  {    public readonly List<Company> ListCompany;    public ReadOnlyModel(string companyInfoPath)    {      XElement companys = XElement.Load(companyInfoPath);      var elements = from e in companys.Elements("company")              where e.Element("name").Value.Equals("C#")              select e;      ListCompany = GetListCompany(elements);    }    /// <summary>    /// 解析xml文檔    /// </summary>    /// <param name="elements"></param>    /// <returns></returns>    private List<Company> GetListCompany(IEnumerable<XElement>elements)    {      var listCompany = new List<Company>();      foreach (var element in elements)      {        var companyModel = new Company()        {          CompanyName = element.Element("name").Value,          CompanyEmail = element.Element("email").Value        };        listCompany.Add(companyModel);      }      return listCompany;    }  }根據(jù)傳入的路徑來解析XML文件賦值給動(dòng)態(tài)常量,能很好的擴(kuò)展應(yīng)用程序的常量值
三、枚舉
枚舉是用戶定義的整數(shù)類型,在聲明一個(gè)枚舉時(shí),要指定該枚舉的實(shí)例可以包含的一組可以接受的值,枚舉具有如下的優(yōu)勢(shì):
1.枚舉可以使代碼更易于維護(hù),有助于確定給變量指定合法的,期望的值
2.枚舉使代碼更清晰,允許用描述性的名稱來表示整數(shù),而不是含義模糊、變化多端的數(shù)
3.枚舉也是代碼更易于輸入
在實(shí)際應(yīng)用中通常在枚舉上面加上Description需要顯示的枚舉特性值,在頁面顯示的時(shí)候通常顯示的也是枚舉的特性值,所以有必要寫一個(gè)獲取枚舉特性值的通用方法
class Program  {    static void Main(string[] args)    {      //ReadOnlyModel readOnlyCompany = new ReadOnlyModel(@"D:/GitHubProject/C#AdvancedProgramming/VariableDefine/VariableDefine/bin/Debug/Test-Parking-SN.xml"); //C:/Program Files(x86)/FPOnline      //foreach (var company in readOnlyCompany.ListCompany)      //{      //  Console.WriteLine("company name is {0} company email is {1}", company.CompanyName, company.CompanyEmail);      //}      //Console.WriteLine("company name is {0} company email is {1}", ConstClass.CompanyName, ConstClass.CompanyEmail);      MemberLevel superMember = MemberLevel.SuperMember;      Console.WriteLine(superMember.GetDescriptionEnum());      Console.ReadKey();    }  }  public enum MemberLevel  {    [Description("超級(jí)會(huì)員")]    SuperMember=1,    [Description("一般會(huì)員")]    Member=2,    [Description("普通用戶")]    GeneralUser=3  }  public static class EnumExtension  {    public static string GetDescriptionEnum(this Enum enumValue)    {      DescriptionAttribute attr = null;      var enumType = enumValue.GetType();      string name = Enum.GetName(enumType, enumValue);      if (name != null)      {        FieldInfo fieldInfo = enumType.GetField(name);        if (fieldInfo != null)          attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;      }      if (attr != null && !string.IsNullOrEmpty(attr.Description))        return attr.Description;      else return string.Empty;    }  }參考博客:枚舉知多少
四、C#的預(yù)處理指令
使用預(yù)處理指令可以禁止編譯器編譯與額外功能相關(guān)的代碼,以控制不同版本擁有的功能,如企業(yè)版和基本版本
#define(給定名稱的符號(hào))和#undef(刪除名稱的符號(hào))一般與#if、#elif、#else、#endif結(jié)合起來使用如

沒有找到預(yù)定義的Debug就不會(huì)執(zhí)行 #if和#endif代碼塊里面的語句,這也稱為條件編譯。
同樣的預(yù)處理器指令有:#warning和#error,當(dāng)編譯器遇到它們時(shí),會(huì)分別產(chǎn)生警告或錯(cuò)誤,如果編譯器遇到#warning指令,會(huì)給用戶顯示#warning后面的文本,之后編譯繼續(xù),如果編譯器遇到#error指令,就會(huì)給用戶顯示后面的文本,作為一條編譯錯(cuò)誤消息,然后立即退出編譯。
#region和#endregion指令用于把一段代碼標(biāo)記為有給定名稱的一個(gè)塊,#line指令用于改變編譯器在警告和錯(cuò)誤信息中顯示的文件名和行號(hào)信息,#pragm可以印制或還原指定的編譯警告參考:https://msdn.microsoft.com/zh-cn/library/yt3yck0x.aspx
新聞熱點(diǎn)
疑難解答
圖片精選