c#:如何从 List<person> 中的特定索引读取 [英] c# : how to read from specific index in List&lt;person&gt;

查看:50
本文介绍了c#:如何从 List<person> 中的特定索引读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类人和列表集合,因为列表包含人类的所有值比如:

I have a class of persons and list collection as list contains all the values of person class such as :

列表 ilist 有 2 个值 [0]={firstname,lastname} .[1]={firstname2,lastname2}

List ilist has 2 values [0]={firstname,lastname} . [1]={firstname2,lastname2}

现在,当我迭代列表时,我可以打印列表,但我想更改列表某些部分的值,例如在索引 1 中,如果我想将 firstname2 的值更改为 firstname3,我无法去做吧 .谁能告诉我如何打印列表,然后在该索引上更改索引的任何值,即 person 类中的 firstname 和 secondname 变量,以便我可以更新我的值谢谢

now when i am iterating into the list i am able to print the list but i want to change the value of some parts of my list e.g in index 1 if i want to change the value of firstname2 to firstname3 i am not able to do it . Can anyone tell me how to print the list and then on that index changing any value of the index , i.e. firstname and secondname variable in the person class so that i can update my values Thanks

推荐答案

根据 msdn 上的文档,您可以使用熟悉的索引运算符(就像您在数组上使用的那样).所以 myList[1].lastname = "new last name"; 应该为你做.

According to the docs on msdn you can use the familiar index operator (like on what you use on arrays). So myList[1].lastname = "new last name"; should do it for you.

文档在这里;http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx

请记住,您需要在访问前进行边界检查.

Keep in mind you need to do bounds checking before access.

这篇关于c#:如何从 List<person> 中的特定索引读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆