自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# const常量詳細(xì)介紹

開發(fā) 后端
這里介紹C# const常量,使用Visual C#在 Main()里面使用IntelliSence插入Constant的相關(guān)field的時(shí)候,發(fā)現(xiàn)ReadonlyInt和 InstantReadonlyInt需要指定Constant的實(shí)例對(duì)象。

C#語(yǔ)言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C# const常量,包括介紹readonly和const所修飾的變量等方面。

一般情況下,如果你需要聲明的常量是普遍公認(rèn)的并作為單個(gè)使用,例如圓周率,黃金分割比例等。你可以考慮使用C# const常量,如:public const double PI = 3.1415926;。如果你需要聲明常量,不過(guò)這個(gè)常量會(huì)隨著實(shí)際的運(yùn)行情況而決定,那么,readonly常量將會(huì)是一個(gè)不錯(cuò)的選擇,例如上面***個(gè)例子的訂單號(hào)Order.ID。

另外,如果要表示對(duì)象內(nèi)部的默認(rèn)值的話,而這類值通常是常量性質(zhì)的,那么也可以考慮const。更多時(shí)候我們對(duì)源代碼進(jìn)行重構(gòu)時(shí)(使用Replace Magic Number with Symbolic Constant),要去除魔數(shù)(Magic Number)的影響都會(huì)借助于const的這種特性。

對(duì)于readonly和const所修飾的變量究竟是屬于類級(jí)別的還是實(shí)例對(duì)象級(jí)別的問(wèn)題,我們先看看如下代碼:

  1. using System;  
  2.  
  3. namespace ConstantLab  
  4. {  
  5. class Program  
  6. {  
  7. static void Main(string[] args)  
  8. {  
  9. Constant c = new Constant(3);  
  10. Console.WriteLine("ConstInt = " + Constant.ConstInt.ToString());  
  11. Console.WriteLine("ReadonlyInt = " + c.ReadonlyInt.ToString());  
  12. Console.WriteLine("InstantReadonlyInt = " + c.InstantReadonlyInt.ToString());  
  13. Console.WriteLine("StaticReadonlyInt = " + Constant.StaticReadonlyInt.ToString());  
  14.  
  15. Console.WriteLine("Press any key to continue");  
  16. Console.ReadLine();  
  17. }  
  18. }  
  19.  
  20. class Constant  
  21. {  
  22. public Constant(int instantReadonlyInt)  
  23. {  
  24. InstantReadonlyInt = instantReadonlyInt;  
  25. }  
  26.  
  27. public const int ConstInt = 0;  
  28.  
  29. public readonly int ReadonlyInt = 1;  
  30.  
  31. public readonly int InstantReadonlyInt;  
  32.  
  33. public static readonly int StaticReadonlyInt = 4;  
  34. }  

使用Visual C#在 Main()里面使用IntelliSence插入Constant的相關(guān)field的時(shí)候,發(fā)現(xiàn)ReadonlyInt和 InstantReadonlyInt需要指定Constant的實(shí)例對(duì)象;而ConstInt和StaticReadonlyInt卻要指定 Constant class(參見上面代碼)??梢?,用const或者static readonly修飾的常量是屬于類級(jí)別的;而readonly修飾的,無(wú)論是直接通過(guò)賦值來(lái)初始化或者在實(shí)例構(gòu)造函數(shù)里初始化,都屬于實(shí)例對(duì)象級(jí)別。

一般情況下,如果你需要表達(dá)一組相關(guān)的編譯時(shí)確定常量,你可以考慮使用枚舉類型(enum),而不是把多個(gè)C# const常量直接嵌入到class中作為field,不過(guò)這兩種方式?jīng)]有絕對(duì)的孰優(yōu)孰劣之分。

  1. using System;  
  2.  
  3. enum CustomerKind  
  4. {  
  5. SuperVip,  
  6. Vip,  
  7. Normal  
  8. }  
  9.  
  10. class Customer  
  11. {  
  12. public Customer(string name, CustomerKind kind)  
  13. {  
  14. m_Name = name;  
  15. m_Kind = kind;  
  16. }  
  17.  
  18. private string m_Name;  
  19. public string Name  
  20. {  
  21. get { return m_Name; }  
  22. }  
  23.  
  24. private CustomerKind m_Kind;  
  25. public CustomerKind Kind  
  26. {  
  27. get { return m_Kind; }  
  28. }  
  29.  
  30. public override string ToString()  
  31. {  
  32. return "Name: " + m_Name + "[" + m_Kind.ToString() + "]";  
  33. }  

【編輯推薦】

  1. C# lock關(guān)鍵字?jǐn)⑹?/FONT>
  2. C#.Net FrameWork簡(jiǎn)介
  3. C# new和override簡(jiǎn)單描述
  4. C#值類型和引用類型淺談
  5. C#標(biāo)識(shí)符簡(jiǎn)單分析
責(zé)任編輯:佚名 來(lái)源: CSDN論壇
相關(guān)推薦

2009-08-27 15:17:40

C# const變量

2009-08-10 16:30:56

C# BitmapDa

2009-08-12 15:34:40

C# DBNull

2011-06-21 10:37:56

const

2009-08-21 15:16:23

C#使用指針

2009-08-24 18:21:23

C# ListView

2009-08-03 18:49:17

C#和Java

2009-08-20 15:26:42

C#循環(huán)語(yǔ)句

2009-08-21 09:23:11

C# GDI+

2009-08-07 16:10:20

C#調(diào)用API

2009-08-13 13:38:30

C#命名規(guī)范

2009-08-14 17:04:50

C#類型系統(tǒng)

2009-08-27 14:32:15

C#編寫ActiveX

2009-08-25 17:28:23

C#創(chuàng)建DataSet

2009-08-06 14:59:36

C#編譯器

2009-08-13 15:40:28

C#基礎(chǔ)知識(shí)

2009-08-27 17:31:44

C#創(chuàng)建Windows

2011-06-08 13:35:18

C#數(shù)據(jù)類型

2017-10-30 16:50:41

Linuxconst

2011-07-20 16:57:05

C++const
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)