12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
编程知识 时间:2024-11-20 12:51:21
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
在C#中,通过key取值可以使用Dictionary的TryGetValue方法或者直接使用[key]索引操作符。使用TryGetValue方法:Dictionary<string, int> dict = new Dictionary<string, int>()
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
在C#中,通过key取值可以使用Dictionary的TryGetValue方法或者直接使用[key]索引操作符。
Dictionary<string, int> dict = new Dictionary<string, int>();dict.Add("key1", 100);dict.Add("key2", 200);int value;if (dict.TryGetValue("key1", out value)){Console.WriteLine("Value for key1: " + value);}
Dictionary<string, int> dict = new Dictionary<string, int>();dict.Add("key1", 100);dict.Add("key2", 200);int value = dict["key1"];Console.WriteLine("Value for key1: " + value);
注意:在使用[key]索引操作符时,如果指定的key不存在于字典中,会抛出KeyNotFoundException异常。因此最好使用TryGetValue方法来避免异常的发生。
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19