How do lists work in c #?

2

I was wondering if someone could guide me or tell me what the concept is called in order to learn it and / or apply it on my own.

Sometimes, using Api's I have found that some method returns a list, for example in this case.

 var ListaDevuelta = api.GetLeaguePositions(RiotSharp.Misc.Region.euw, api.GetSummonerByName(RiotSharp.Misc.Region.euw, Cargar[it+2]).Id);

And this list has ... Properties? to which I agree with ListaDevuelta.First().Nombredepropiedad.

I would like to understand what first() does, and since it is possible that a list has properties, how could I create a list that had properties, since I was interested in creating a list that saved names, emails, passwords, etc. .

I would like to know how I could create a list with these properties, for example, a list, with property name, email etc and that bring such data.

Ejemplo.First().Nombre

Thank you very much for all your help.

Edit:

private class Idunno
{
    public List<string> nombre;

}

private void Form1_Load(object sender, EventArgs e)
{
    List<Idunno> Lista = new List<Idunno>();
    Idunno objeto = new Idunno();

    string[] prueba = { "Raquel", "sofia", "Juan", "jorge" };

    for(int it = 0; it < prueba.Count(); it++)
    {
       objeto.nombre.Add(prueba[it]);
    }

    Lista.Add(objeto);

    Console.WriteLine(Lista.First().nombre);
}

How could you fill that object with the names of the array, and not just with a single data?

Last edition:

    
asked by Georgia Fernández 12.10.2018 в 15:51
source

1 answer

5

I explain is not that the list has those properties, when you use .First() you are getting the first element of a list, that element is a objeto and that objeto is the one that has the properties.

Now for what I understand you want to do the best way is to create a List<Clase> listaObjetos = new List<Clase>(); to that list you can save objects that are of that class and access the properties of objetos saved.

    private class Ejemplo
    {
        public int id;
        public string descripcion;
    }

    private void crearListaObjeto()
    {
        List<Ejemplo> listaObjetos = new List<Ejemplo>();
        Ejemplo objeto = new Ejemplo();
        objeto.id = 1;
        objeto.descripcion = "Prueba";
        listaObjetos.Add(objeto);
        int isId = listaObjetos.First().id;
        string isDescripcion = listaObjetos.First().descripcion;
    }

For what you need you should apply this:

    private class Idunno
    {
        public string nombre;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<Idunno> Lista = new List<Idunno>();
        string[] prueba = { "Raquel", "sofia", "Juan", "jorge" };
        foreach (string nom in prueba)
        {
            Idunno objeto = new Idunno();
            objeto.nombre = nom;
            Lista.Add(objeto);
        }
        Console.WriteLine(Lista.First().nombre);
    }

In each cycle you must create a objeto with your class type to that object you assign the information and save it in the list.

This is an example of how to initialize a list with predefined objects, I add it because something tells me you are going to need it:

    private class Idunno
    {
        public int id;
        public string nombre;

        public Idunno()
        {

        }

        public Idunno(int isId, string isNombre)
        {
            id = isId;
            nombre = isNombre;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        List<Idunno> Lista = new List<Idunno>() {
            new Idunno(1, "Raquel"),
            new Idunno(2, "sofia"),
            new Idunno(3, "Juan"),
            new Idunno(4, "jorge")
        };
        Console.WriteLine(Lista.First().nombre);
    }
    
answered by 12.10.2018 / 16:58
source