110,538
社区成员
发帖
与我相关
我的任务
分享
namespace dic2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
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")
{
list[i].Value = "hello";//报错
MessageBox.Show(list[i].Value, "out");
}
}
}
}
}
namespace dic2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
struct hello
{
public int a;
public string b;
public hello(int A, string B)
{
a = A;
b = B;
}
}
private void button1_Click(object sender, EventArgs e)
{
Dictionary<int, hello> datatable = new Dictionary<int, hello>();
hello hi=new hello(1,"hello1");
datatable.Add(1, hi);
hi=new hello(2,"hello2");
datatable.Add(2, hi);
int[] keys = datatable.Keys.ToArray();
for(int i=0;i<keys.Length;i++)
{
if(datatable[keys[i]].a==1)
{
datatable[keys[i]].b = "ok";//出错,无法修改该处,因为左侧不是变量(系统提示)
MessageBox.Show(datatable[keys[i]].b, "");
}
}
}
}
}
Dictionary<int, string> datatable = new Dictionary<int, string>();
datatable.Add(1, "hello 1");
datatable.Add(2, "hello 2");
int[] keys = datatable.Keys.ToArray();
for (int i = 0; i < keys.Length; i++)
{
if (datatable[keys[i]] == "hello 2")
{
datatable[keys[i]] = "hello";
}
}