有沒有辦法一次爲Dictionary<string, int>
找到一個鍵的值?現在我正在打兩個電話。詞典包含鍵和在一個函數中獲取值
if(_dictionary.ContainsKey("key") {
int _value = _dictionary["key"];
}
我想要做它喜歡:
object _value = _dictionary["key"]
//but this one is throwing exception if there is no such key
我想如果返回null不存在這樣的鍵或一個電話獲得的價值?
如果字典包含帶有該鍵的元素,則TryGetValue將返回true,否則返回false。 – Femaref
如果他有一個像{「key」,0}這樣的鍵/值對,並且他試圖從一個不存在的鍵中獲取該值,那麼該int值將爲0。 – terrybozzio
@terrybozzio在這種情況下'exists'將會是'false' –