我有一类人员和列表集合,因为列表包含人员类的所有值,例如:
列表 ilist 有 2 个值 [0]={firstname,lastname} 。[1]={名2,姓2}
现在,当我迭代列表时,我可以打印列表,但我想更改列表中某些部分的值,例如在索引 1 中,如果我想将 firstname2 的值更改为 firstname3,我无法做到. 谁能告诉我如何打印列表,然后在该索引上更改索引的任何值,即 person 类中的 firstname 和 secondname 变量,以便我可以更新我的值 谢谢
我有一类人员和列表集合,因为列表包含人员类的所有值,例如:
列表 ilist 有 2 个值 [0]={firstname,lastname} 。[1]={名2,姓2}
现在,当我迭代列表时,我可以打印列表,但我想更改列表中某些部分的值,例如在索引 1 中,如果我想将 firstname2 的值更改为 firstname3,我无法做到. 谁能告诉我如何打印列表,然后在该索引上更改索引的任何值,即 person 类中的 firstname 和 secondname 变量,以便我可以更新我的值 谢谢
根据 msdn 上的文档,您可以使用熟悉的索引运算符(就像您在数组上使用的那样)。所以myList[1].lastname = "new last name";应该为你做。
文档在这里;http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx
请记住,您需要在访问之前进行边界检查。
我在谷歌上搜索对象数组值 C# 中的访问特定索引时来到这里,但却遇到了这个非常令人困惑的问题。现在,对于那些正在寻找类似解决方案的人(获取对象 IList 的特定字段,其中也包含数组)。与 OP 在他的问题中解释的内容非常相似,您有 IList 人员,并且人员包含名字、姓氏、单元格等,并且您想要获取人员 1 的名字。这是您的操作方法。
假设我们有
IList<object> myMainList = new List<object>();
myMainList.Add(new object[] { 1, "Person 1", "Last Name 1" });
myMainList.Add(new object[] { 2, "Person 2", "Last Name 2" });
起初,我虽然这样做可以解决问题:
foreach (object person in myMainList)
{
string firstname = person[1].ToString() //trying to access index 1 - looks right at first doesn't it??
}
但是意外惊喜,C#编译器报错
无法使用 [] 将索引应用于“对象”类型的表达式
菜鸟的错误,但我的头撞到了墙上。这是正确的代码
foreach (object[] person in myMainList) //cast object[] NOT object
{
string firstname = person[1].ToString() //voila!! we have lift off :)
}
这适用于像我这样因同样的错误而陷入困境的新手。它发生在我们最好的人身上。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
List<Person> list = new List<Person>();
Person oPerson = new Person();
oPerson.Name = "Anshu";
oPerson.Age = 23;
oPerson.Address = " ballia";
list.Add(oPerson);
oPerson = new Person();
oPerson.Name = "Juhi";
oPerson.Age = 23;
oPerson.Address = "Delhi";
list.Add(oPerson);
oPerson = new Person();
oPerson.Name = "Sandeep";
oPerson.Age = 24;
oPerson.Address = " Delhi";
list.Add(oPerson);
int index = 1; // use for getting index basis value
for (int i=0; i<list.Count;i++)
{
Person values = list[i];
if (index == i)
{
Console.WriteLine(values.Name);
Console.WriteLine(values.Age);
Console.WriteLine(values.Address);
break;
}
}
Console.ReadKey();
}
}
class Person
{
string _name;
int _age;
string _address;
public String Name
{
get
{
return _name;
}
set
{
this._name = value;
}
}
public int Age
{
get
{
return _age;
}
set
{
this._age = value;
}
}
public String Address
{
get
{
return _address;
}
set
{
this._address = value;
}
}
}
}
可以使用它们的indexer直接修改列表。
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
var list = new List<Person>
{
new Person
{
FirstName = "Bob",
LastName = "Carlson"
},
new Person
{
FirstName = "Elizabeth",
LastName = "Carlson"
},
};
// Directly
list[1].FirstName = "Liz";
// In a loop
foreach(var person in list)
{
if(person.FirstName == "Liz")
{
person.FirstName = "Lizzy";
}
}
我看不到您可以在哪里遇到问题:
public class Persons
{
public Persons(string first, string last)
{
this.firstName = first;
this.lastName = last;
}
public string firstName { set; get; }
public string lastName { set; get; }
}
...
List<Persons> lst = new List<Persons>();
lst.Add(new Persons("firstname", "lastname"));
lst.Add(new Persons("firstname2", "lastname2"));
for (int i = 0; i < lst.Count; i++)
{
Console.Write("{0}: {2}, {1}", i, lst[i].firstName, lst[i].lastName);
if (i == 1)
{
lst[i].firstName = "firstname3";
lst[i].lastName = "lastname3";
Console.Write(" --> {1}, {0}", lst[i].firstName, lst[i].lastName);
}
Console.WriteLine();
}
}
输出:
0: lastname, firstname
1: lastname2, firstname2 --> lastname3, firstname3
有关您的要求/为什么以这种方式访问列表的更多信息可能有助于提供更好的方法建议,但是:
IndexOf(). (注意这将循环数组以找到对象的位置)