c# : how to read from specific index in List<person>
Solution 1
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.
Docs are here; http://msdn.microsoft.com/en-us/library/0ebtbkkc.aspx
Keep in mind you need to do bounds checking before access.
Solution 2
I came here whilst searching for access specific index in object array values C# on Google but instead came to this very confusing question. Now, for those that are looking for a similar solution (get a particular field of an object IList that contains arrays within it as well). Pretty much similar to what the OP explained in his question, you have IList person and person contains firstname, lastname, cell etc and you want to get the firstname of person 1. Here is how you can do it.
Assume we have
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" });
At first, I though this would do the trick:
foreach (object person in myMainList)
{
string firstname = person[1].ToString() //trying to access index 1 - looks right at first doesn't it??
}
But surprise surprise, C# compiler complains about it
Cannot apply indexing with [] to an expression of type 'object'
Rookie mistake, but I was banging my head against the wall for a bit. Here is the proper code
foreach (object[] person in myMainList) //cast object[] NOT object
{
string firstname = person[1].ToString() //voila!! we have lift off :)
}
This is for any newbie like me that gets stuck using the same mistake. It happens to the best of us.
Solution 3
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;
}
}
}
}
user2740970
Updated on June 21, 2020Comments
-
user2740970 almost 4 years
I have a class of persons and list collection as list contains all the values of person class such as :
List ilist has 2 values [0]={firstname,lastname} . [1]={firstname2,lastname2}
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
-
user2740970 over 10 yearsmy code is IList _ilist= new ArrayList(); FileReader(fileB, ref _ilist); and i am reading data from the file and adding into the list
-
Jeppe Stig Nielsen over 10 years@user2740970 Try to use generic
List<Person>
instead ofArrayList
class. It will make life easier for you. -
user2740970 over 10 yearsi have posted a new question here because i am not able to answer my question stackoverflow.com/questions/18598129/…
-
evanmcdonnal over 10 years@user2740970 your question is unclear then. In the title you say you're using
List<Person>
which means you're using the generic list (List<T>
) with the type argument as Person.ArrayList
is a different class entirely and in general is not used in new code. -
Joshua Dwire over 8 yearsConsider expanding your answer to explain to the asker why this achieves the desired result, possibly linking to documentation. As is, this is only marginally useful.
-
Moeez about 5 years
foreach (object[] person in myMainList)
this gives meCannot convert type 'ConsoleApplication1.Readings' to 'object[]'
error -
TheDanMan about 5 years@Faisal, looks like you are using a concrete type and not object[]. Rather use "foreach (var person in myMainList)" if using the new c#