C#泛型键值对集合
//泛型键值对集合
Dictionary<int, string> dictionary = new Dictionary<int, string>()
{
{ 1,"打法"},
{ 4,"搜索"},
};
dictionary.Add(2, "你");
dictionary.Add(5, "哈哈");
dictionary.Add(10, "喜喜");
string valuel = dictionary[2];
Console.WriteLine(valuel);
//索引器赋值
dictionary[3] = "拉拉";
dictionary[2] = "萨顶顶";
Console.WriteLine(dictionary[2]);
foreach (KeyValuePair<int, string> item in dictionary)
{
Console.WriteLine("键是{0},值是{1}", item.Key, item.Value);
}
//装箱:比如将 int string的对象强转为Object对象
int i = 1;
object p = 1;
//拆箱 将objectg 通过显示类型转换为int类型的变量
object o = 1;
int n = (int)o;
//都会产生较大的性能损耗
//arraylist存在不安全类型的装箱拆箱缺点,所以在C#2.0以后,出现了泛型的概念