数组插入的问题
下面这这样写对吗?为什么向数组中入数据之后都数组中的所有数据都是最后一个插入的那个数?
List<Model.Model_UserProjectID> list = new List<Model_UserProjectID>();
Model.Model_UserProjectID model = new Model_UserProjectID();
foreach (String str in UserIdList)
{
model.ProjId = int.Parse(ProjId);
model.UserId = int.Parse(str);
list.Add(model);
}
list
------解决方案--------------------List<Model.Model_UserProjectID> list = new List<Model_UserProjectID>();
foreach (String str in UserIdList)
{
Model.Model_UserProjectID model = new Model_UserProjectID();//放循环里面
model.ProjId = int.Parse(ProjId);
model.UserId = int.Parse(str);
list.Add(model);
}
------解决方案--------------------为什么不这样呢?
List<Model.Model_UserProjectID> list = new List<Model_UserProjectID();
foreach (String str in UserIdList)
{
Model.Model_UserProjectID model = new Model_UserProjectID();
model.ProjId = int.Parse(ProjId);
model.UserId = int.Parse(str);
list.Add(model);
}
------解决方案--------------------
+1 必须在循环