Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given:
public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

If I later define a List<> of Person Objects:
List<Person> people = new List<Person>();

I know I can process individual Person objects in the List using an index, for example:
Person mom = people[0];

BUT, can I access individual properties of a Person as follows?:
string fname = people[0][0];


What I have tried:

Using LINQPad 5...

void Main()
{
	
	List<Person> people = new List<Person>();
	people.Add(new Person { FirstName = "Mark", LastName = "Miller" });
	people.Add(new Person { FirstName = "Nancy", LastName = "Miller" });
	
	Person mom = people[1];
	
// this line fails... is there a way around this?
	string fname = mom.Properties[0];

}

public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
Posted
Updated 17-Aug-20 7:12am

No, you would need to use the property name:
C#
string fname = people[0].FirstName;
The syntax you're suggesting would only work if your class had an indexer:
C#
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    
    public string this[int index]
    {
        get
        {
            switch (index)
            {
                case 0: return FirstName;
                case 1: return LastName;
                default: throw new IndexOutOfRangeException();
            }
        }
    }
}
...
string fname = people[0][0];
Indexers - C# Programming Guide | Microsoft Docs[^]

If you want to iterate over all of the properties in your class, you'd have to use Reflection or the TypeDescriptor.
Reflection (C#) | Microsoft Docs[^]
TypeDescriptor Class (System.ComponentModel) | Microsoft Docs[^]

For example:
C#
using System.Collections.Generic;
using System.ComponentModel;
...
public static void DumpList<T>(IEnumerable<T> list)
{
    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
    foreach (T item in list)
    {
        Console.WriteLine("{");
        foreach (PropertyDescriptor property in properties)
        {
            Console.WriteLine("    {0} = {1}", property.Name, property.GetValue(item));
        }
        Console.WriteLine("}");
    }
}
Usage:
C#
List<Person> people = new List<Person>
{
    new Person { FirstName = "Mark", LastName = "Miller" },
    new Person { FirstName = "Nancy", LastName = "Miller" }
};

DumpList(people);
 
Share this answer
 
v2
Thanks Richard - I could modify my classes to include an index property.
Working on a simple way of dumping contents of Lists to a simple CSV file for review during debugging.

Instead of having multiple "dump" methods, I'd like only one.
Thanks again.
 
Share this answer
 
Comments
Richard Deeming 18-Aug-20 4:50am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.
Mark Miller 18-Aug-20 8:16am    
Got it, thanks, and thanks for the Indexer idea. Works like a charm.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900