Foreach and custom objects
Is it possible to print a property of a custom object in a foreach loop ? The custom object is Person and has a property called Name. I want to print all names of the persons in the personslist (PList) This is the standard way of doing it. It is good, no doubt about that. foreach (Person X in PList) //good one { Console.WriteLine(X.Name); } Some where I have seen that people used something like this. If you know you are only interrested in the string Name, you could do it. foreach (Person.Name Y in PList) { Console.WriteLine(Y); } foreach (var Z.Name in PList) { Console.WriteLine(Z); } https://code.sololearn.com/cmysAzng96Fx Both the second and third foreach are not working for obvious reason. It there a way to do it like that ?