I'm a newbie in c# and would like to ask you something, please help.
I created a variable with 3 persons inside using anonymous classes like this:
var personas = new[] {
new {nombre = "Guillermo", apellido = "Pérez", edad = 37}
, new {nombre = "Juan", apellido = "García", edad = 27}
, new {nombre = "Maria", apellido = "Rosina", edad = 47}
};
Now, I tried to iterate on each of these items and I did (using the following code)
for (int i = 0; i < personas.Length; i++)
{
var persona = personas[i];
Console.WriteLine("{0} {1} tiene {2} años.", persona.nombre, persona.apellido, persona.edad);
}
THE PROBLEM comes when I want to create a method to do what I did with the last piece of code, the idea is to create a method and pass this object (in this case personas) to the method and do the loop inside. What I did is this:
showPersonas(personas);
static void showPersonas(object[] personsList) {
for (int i = 0; i < personsList.Length; i++) {
var algo = personsList[i];
Console.WriteLine(personsList[i].nombre); ----> ERROR!!
}
}
What I would like the method to do is to do what I did with the for loop, to be able to receive the object, iterate and print each item.
THANK YOU! for all your attention.
dynamicor reflection. Create a class and use as array (orIEnumerable<>) of that as the type of your input parameter.