C# Dictionary用法总结
发布时间:2021-07-06 05:55:28 所属栏目:大数据 来源: https://blog.csdn.net/kasama1
导读:C# Dictionary用法总结 Posted on? 2011-12-09 18:06 ? work hard work smart?阅读( 69002 ) 评论( 3 )? 编辑? 收藏 C# Dictionary用法总结 1、用法1: 常规用 增加键值对之前需要判断是否存在该键,如果已经存在该键而且不判断,将抛出异常。 所以这样每次
C# Dictionary用法总结 Posted on?
2011-12-09 18:06?
work hard work smart?阅读(
69002) 评论(
)?
编辑?
收藏
C# Dictionary用法总结
1、用法1: 常规用
增加键值对之前需要判断是否存在该键,如果已经存在该键而且不判断,将抛出异常。所以这样每次都要进行判断,很麻烦,在备注里使用了一个扩展方法
|
public
?
static
?
void
?
DicSample1()
{
?
????
Dictionary<String,String> pList =
new
?
????
try
{
????????
if
?
(pList.ContainsKey(
"Item1"
) ==
false
)
????????
{
????????????
pList.Add(
,
"ZheJiang"
);
}
"Item2"
)==
)
{
"ShangHai"
);
}
else
{
pList[
] =
;
}
"Item3"
)
{
"BeiJiang"
);
}
?????????
}
catch?
(System.Exception e)
{
Console.WriteLine(
"Error: {0}"
}
???
?
????
))
{
"Output: "
?
+ pList[
]);
}
?
//遍历Key
foreach
?
(
var
?
key
in
?
pList.Keys)
{
"Output Key: {0}"
(String value
pList.Values)
{
"Output Value: {0}"
//遍历Key和Value
dic
pList)
{
"Output Key : {0},Value : {1} "
}
}
@H_646_301@
2、用法2:Dictionary的Value为一个数组
/// <summary>
/// Dictionary的Value为一个数组
/// </summary>
?
DicSample2()
?
{
?????
"Huzhou"
"HangZhou"
"TaiZhou"
?
};
String[] ShangHai = {
"Budong"
"Buxi"
?
};
dic.Add(
"ZJ"
"SH"
?
+ dic[
][0]);
}
@H_646_301@
3、用法3: Dictionary的Value为一个类
DicSample3()
{
null
;
?????
for
?
int
?
i = 0; i < 3; i++ )
{
?????????
stu =
Student();
stu.Name = i.ToString();
stu.Name =
"StuName"
?
+ i.ToString();
stuList.Add(i.ToString(),stu);
}
?
student
stuList)
{
"Output : Key {0},Num : {1},Name {2}"
}
@H_646_301@
Student类:
class?
Student
public
?
String Num {
get
;
set
; }
String Name {
; }
}
@H_646_301@
?4 备注:Dictionary的扩展方法使用
?
/// Dictionary的扩展方法使用
DicSample4()
?????
Dictionary<
int
DictionaryExtensionMethodClass.TryAdd(dict,1,monospace!important; font-size:12px!important; min-height:inherit!important; color:blue!important; background:none!important">"ZhangSan"
);
"WangWu"
DictionaryExtensionMethodClass.AddOrPeplace(dict,3,monospace!important; font-size:12px!important; min-height:inherit!important; background:none!important">);
"ZhangWu"
);
"LiSi"
);
//2)TryAdd 和 AddOrReplace 这两个方法具有较强自我描述能力,用起来很省心,而且也简单:
dict.AddOrPeplace(20,monospace!important; font-size:12px!important; min-height:inherit!important; color:blue!important; background:none!important">"Orange"
);
dict.TryAdd(21,monospace!important; font-size:12px!important; min-height:inherit!important; color:blue!important; background:none!important">"Banana"
);
dict.TryAdd(22,monospace!important; font-size:12px!important; min-height:inherit!important; color:blue!important; background:none!important">"apple"
);
?
//3)像Linq或jQuery一样连起来写??
dict.TryAdd(10,monospace!important; font-size:12px!important; min-height:inherit!important; color:blue!important; background:none!important">"Bob"
)
.TryAdd(11,monospace!important; font-size:12px!important; min-height:inherit!important; color:blue!important; background:none!important">"Tom"
)
.AddOrPeplace(12,monospace!important; font-size:12px!important; min-height:inherit!important; color:blue!important; background:none!important">"Jom"
);
?
//4) 获取值
String F =
"Ba"
;
dict.TryGetValue(31,
out
?
F);
"F : {0}"
{
"Output : Key : {0},Value : {1}"
}
//5)下面是使用GetValue获取值
v1 = dict.GetValue(111,monospace!important; font-size:12px!important; min-height:inherit!important; background:none!important">);
v2 = dict.GetValue(10,monospace!important; font-size:12px!important; min-height:inherit!important; color:blue!important; background:none!important">"abc"
);
?
//6)批量添加
dict1 =
>();
dict1.AddOrPeplace(3,3);
dict1.AddOrPeplace(5,5);
?
dict2 =
>();
dict2.AddOrPeplace(1,1);
dict2.AddOrPeplace(4,4);
dict2.AddRange(dict1,monospace!important; font-size:12px!important; min-height:inherit!important; background:none!important">);
}
@H_646_301@
扩展方法所在的类
DictionaryExtensionMethodClass
????
/// <summary>
/// 尝试将键和值添加到字典中:如果不存在,才添加;存在,不添加也不抛导常
/// </summary>
static
?
Dictionary<TKey,TValue> TryAdd<TKey,TValue>(
this
?
)
dict.Add(key,value);
return
?
dict;
}
?
/// <summary>
/// 将键和值添加或替换到字典中:如果不存在,则添加;存在,则替换
/// </summary>
dict[key] = value;
dict;
}
?
/// <summary>
/// 获取与指定的键相关联的值,如果没有则返回输入的默认值
/// </summary>
TValue GetValue<TKey,TValue defaultValue)
dict.ContainsKey(key)?dict[key] : defaultValue;
?
/// <summary>
/// 向字典中批量添加键值对
/// </summary>
/// <param name="replaceExisted">如果已存在,是否替换</param>
bool
?
replaceExisted)
{
item
values)
{
????????????
(dict.ContainsKey(item.Key) ==
false
?
|| replaceExisted)
????????????????
dict[item.Key] = item.Value;
}
dict;
}
?
?
}
@H_646_301@ (编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!