以自己為例,用 Java 的 HashMap 用習慣了,對應到 C# 的 Dictionary 一直不知道怎麼取值。

C# 的語法比較特別,取陣列裡的值,'[' ']' 除了代入數值外,大部分的 API 支援 在裡頭填字串。

範例:

using System.Collections.Generic;

Dictionary<string, int> dic = new Dictionary<string, int>();

int myValue = dic["cat"];

int myNewValue = 7 ;
dic["cat"] = myNewValue;  // 修改值.

 

參考:how to update the value stored in Dictionary in C#?

 

arrow
arrow

    Robert 發表在 痞客邦 留言(0) 人氣()