[C#] STL 的 initial Value / default value

  • 1408
  • 0

摘要:[C#] STL 的 initial Value / default value

最近常常在幾個 STL 跑來跑去,

常常忘了要怎麼在宣告時就做 initial value 的動作,

這篇就記錄一下幾種常用的 STL 和 initial Value 的方式吧,

- List

網址 : http://www.dotnetperls.com/initialize-list

List<string> listData = new List<string> () {"1", "2"};

- Dictionary / HashTable

網址 : http://msdn.microsoft.com/en-us/library/bb531208.aspx

網址 : http://stackoverflow.com/questions/128343/is-there-a-better-way-to-initialize-a-hastable-in-net-without-using-add-method

Dictionary<string, string> dictData = new Dictionary<string, string> ()

{

    {"1", "1"}

    , {"2", "2"}

    , {"3", "3"}

};

好吧, 這篇好像太短了, 

那就再加上一點點吧,

我們一般會使用 default 這個 keyword 去取得預設值,

那每個 value 的 default 結果為何呢?

網址 : http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

Value type

Default value

bool

false

byte

0

char

'\0'

decimal

0.0M

double

0.0D

enum

The value produced by the expression (E)0, where E is the enum identifier.

float

0.0F

int

0

long

0L

sbyte

0

short

0

struct

The value produced by setting all value-type fields to their default values and all reference-type fields to null.

uint

0

ulong

0

ushort

0

 

======

鸞鳳翎毛雨壓垂,

此時應被雀輕欺,

忽朝一日雲霄霽,

依舊還教振羽衣。

======

歡迎轉錄文章,

但記得保留網址和作者名稱~