<在线waiting。。。>dictionary的遍历问题

Jellyfancy 2010-07-08 11:06:07


Dictionary<int, string> datatable = new Dictionary<int, string>();

datatable.Add(1,"hello 1");

datatable.Add(2,"hello 2");
for (int j = 0; j < datatable.Count - 1; j++)
{
//输出当前项目的value
if(该value="hello 2")
{
messagebox.show("ok","out");
}
}

请问红字部分怎么实现?
...全文
159 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jellyfancy 2010-07-08
  • 打赏
  • 举报
回复
5楼可行,多谢多谢。。。结贴给分
zhengqian529 2010-07-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyazhe 的回复:]
C# code

Dictionary<int, string> datatable = new Dictionary<int, string>();
datatable.Add(1, "hello 1");
datatable.Add(2, "hello 2");
var list = datatable.ToList();
for (int i = 0; i……
[/Quote]

up
兔子-顾问 2010-07-08
  • 打赏
  • 举报
回复

Dictionary<int, string> datatable = new Dictionary<int, string>();
datatable.Add(1, "hello 1");
datatable.Add(2, "hello 2");
var list = datatable.ToList();
for (int i = 0; i < list.Count; i++)
{
if (list[i].Value == "hello 2")
{
MessageBox.Show("ok", "out");
}
}
Jellyfancy 2010-07-08
  • 打赏
  • 举报
回复
foreach可以遍历,我想对其遍历的时候,当满足一定条件时对其修改,就不能呢公用foreach了,还有别的遍历吗?用for的
rqx110 2010-07-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhgroup 的回复:]

foreach(KeyValuePair(int,string) key in datatable)
{
key.Key,key.Value
}

KeyValuePair这个记不清了,查一下SDK吧
[/Quote]

foreach(KeyValuePair<int,string> kv in datatable)
{
kv.Key,kv.Value
}
zzyhuian06142 2010-07-08
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;

class DictionaryDemo
{
static void Main(string[] args)
{
DictionaryDemo001();
Console.ReadLine();

DictionaryDemo002();
Console.ReadLine();

DictionaryDemo003();
Console.ReadLine();
}

/// <summary>
/// 一般用法
/// </summary>
static void DictionaryDemo001()
{
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(1, "111");
dict.Add(2, "222");

//判断是否存在相应的key并显示
if (dict.ContainsKey(2))
{
Console.WriteLine(dict[2]);
}

//遍历Keys
foreach (var item in dict.Keys)
{
Console.WriteLine("Key:{0}", item);
}

//遍历Values
foreach (var item in dict.Values)
{
Console.WriteLine("value:{0}", item);
}

//遍历整个字典
foreach (var item in dict)
{
Console.WriteLine("key:{0} value:{1}", item.Key, item.Value);
}
}

/// <summary>
/// 参数为其它类型
/// </summary>
static void DictionaryDemo002()
{
Dictionary<string, string[]> dict = new Dictionary<string, string[]>();
dict.Add("1", "1,11,111".Split(','));
dict.Add("2", "2,22,222".Split(','));
Console.WriteLine(dict["2"][2]);
}

/// <summary>
/// 调用自定义类
/// </summary>
static void DictionaryDemo003()
{
Dictionary<int, yongfa365> dict = new Dictionary<int, yongfa365>();
for (int i = 0; i < 10; i++)
{
yongfa365 y = new yongfa365();
y.UserCode = i;
y.UserName = "www.yongfa365.com " + i.ToString();
dict.Add(i, y);
}
foreach (var item in dict)
{
Console.WriteLine("{0} One:{1} UserName:{2}", item.Key, item.Value.UserCode, item.Value.UserName);
}
}
}

class yongfa365
{
public int UserCode { get; set; }
public string UserName { get; set; }

}
zhgroup 2010-07-08
  • 打赏
  • 举报
回复
foreach(KeyValuePair(int,string) key in datatable)
{
key.Key,key.Value
}

KeyValuePair这个记不清了,查一下SDK吧

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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