我想问下,怎么样删除和增加Dictionary中的 键值对

makeppy 2008-12-08 09:30:33
RT 我想要个例子 谢谢指点啦O(∩_∩)O哈哈~
...全文
463 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangyanyang 2009-06-09
  • 打赏
  • 举报
回复
mark
zhyuanshan 2008-12-08
  • 打赏
  • 举报
回复

openWith.Remove("key");
openWith.Add("key","value");
jiang_jiajia10 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 makeppy 的回复:]
我还有个问题:在key不变的时候如何改变它的value值啊?
[/Quote]

Dictionary<string, string> openWith =
new Dictionary<string, string>();
int i=0;

if (openWith .Contains(i))
{
openWith [i] = i; //修改key为i的Value
}
else
{
openWith .Add(i, 0); //添加key为i的记录
}

jiang_jiajia10 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 makeppy 的回复:]
我还有个问题:在key不变的时候如何改变它的value值啊?
[/Quote]

Dictionary<string, string> openWith =
new Dictionary<string, string>();
int i=0;

if (openWith .Contains(i))
{
openWith [i] = i; //修改key为i的Value
}
else
{
openWith .Add(i, 0); //添加key为i的记录
}

makeppy 2008-12-08
  • 打赏
  • 举报
回复
我还有个问题:在key不变的时候如何改变它的value值啊?
jiang_jiajia10 2008-12-08
  • 打赏
  • 举报
回复

Dictionary<string, string> openWith =
new Dictionary<string, string>();

openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");


try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}


删除

Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");

if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}


MSDN上的例子参照一下
feiyun0112 2008-12-08
  • 打赏
  • 举报
回复
// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("doc", "word.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}



// Use the Remove method to remove a key/value pair.

Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");

if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}



*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
liwei920 2008-12-08
  • 打赏
  • 举报
回复
Dictionary<int, string> kk = new Dictionary<int, string>();
长沙三毛 2008-12-08
  • 打赏
  • 举报
回复
            Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(1, "11");
d.Add(2, "22");
d.Remove(1);

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧