Instance individual class objects in a list

4

I try to instantiate several objects of the class Persona , which has three attributes, what should be the correct syntax to be able to instantiate it directly in the constructor of the list of people?

   // clase persona
   public class persona
   {
         public string Nombre{ get; set; }
         public string Apellidos{ get; set; }
         public int Edad { get; set; }
   }

   // lista
   List<persona> lista = new List<persona>();

I would like to be able to create them within the constructor itself although I have tried with the method Add() and I have not known how to do it, how do you tell the Add () method what attribute do you want to write?

I guess it would be a complete rookie question, but I've been trying for a while and I have not managed to do it correctly.

    
asked by Edulon 27.10.2017 в 19:45
source

3 answers

6

1- Initializing the objects in the initialization of the list:

This is called Initializer of collections , where in the initialization you can define the default elements without using the Add(T) method directly:

 List<Personas> personas = new List<Personas>{
       new Personas { Nombre = "Einer", Apellido = "Einer", Edad = 17 },
       new Personas { Nombre = "Einer", Apellido = "Einer", Edad = 23 },
       new Personas { Nombre = "Einer", Apellido = "Einer", Edad = 28 }
    };

    Console.WriteLine(personas.Count);// 3

2 - Initializing the class in the method .Add(T) of the list:

And this is Object Initializer where the object's data is initialized in the initialization (which is the same as in the first example);

var personas = new List<Personas();
personas.Add(new Personas{ Nombre = "Einer" , Apellido = "Einer", Edad = 25 });
    
answered by 27.10.2017 / 20:35
source
3

You can start by creating the object by assigning its variables directly.

var obj = new persona(){
Nombre = "Edulon",
Apellidos = "Martinez Rivera",
Edad = 20
}

And to load them in the last place of the list it would be with

lista.add(obj);

Protip Understanding these concepts you can directly add an object to the list by typing in each attribute at a time.

lista.add(new persona(){
    Nombre = "Edulon",
    Apellidos = "Martinez Rivera",
    Edad = 20
    });
    
answered by 27.10.2017 в 20:03
2

The answers added above are correct, and use the modern object creation syntax.

I leave this as clarification.

Before, to add an object to the list, you had previously to create the object you wanted, fill in its properties, and then add it to the list.

The code that will end up producing the compiler is the same, but the way to do it is more explicit.

It was done in the following way (and beyond the warning of the modern compiler, it goes without problems).

List<persona> lista = new List<persona>();
persona personanueva = new persona();
personanueva.Nombre = "Gonzalo";
personanueva.Apellido = "B";
personanueva.Edad = "20";
lista.add(personanueva);

This clarification is for novice users, so they understand how the other answers work internally.

    
answered by 27.10.2017 в 20:58