Printing widgets from a list

Asked

Viewed 6,013 times

1

Is there any way to read the values that are in a list and present them on the console?

I tried it like this, but it doesn’t work

Console.WriteLine(lista);
  • Do any of the answers meet the request? If yes, you can mark one of them as correct. Otherwise, you can leave a comment from the authors asking for further clarification.

2 answers

11

You have to go through all the items in the list and print them out.

foreach(var elemento in lista)
{
    Console.WriteLine(elemento);
}

If you want a version one-Liner. This only works for List, nothing more.

lista.ForEach(Console.WriteLine);

See working on . NET Fiddle.

Warning: If the list is objects, this will call the method ToString() objects. If this is not the desired behavior, you can print property by property, or even create a method that returns a string with the properties you want to show.

Example using the properties

foreach(var elemento in lista)
{
    Console.WriteLine(elemento.Propriedade);
    Console.WriteLine(elemento.OutraPropriedade);
}

0

Foreach([tipo lista] lista on [nome da lista]{

    Console.WriteLine(lista);

}

Where [List type] is the type (string, float,double,int, Object) and list name is the name you gave, example:

List<string> listaString = new List<String>();
Foreach(string lista on listaString {
    Console.WriteLine(lista);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.