I have a list of list of strings:
var list = new List<string> {"apples","peaches", "mango"};
Is there a way to iterate through the list and display the items in a console window without using foreach loop may be by using lambdas and delegates.
I would like to the output to be like below each in a new line:
The folowing fruits are available:
apples
peaches
mango
You can use String.Join
to concatenate all lines:
string lines = string.Join(Environment.NewLine, list);
Console.Write(lines);
By far the most obvious is the good old-fashioned for
loop:
for (var i = 0; i < list.Count; i++)
{
System.Console.WriteLine("{0}", list[i]);
}
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i])
}
I love this particular aspect of linq
list.ForEach(Console.WriteLine);
It's not using a ForEach loop per se as it uses the ForEach actor. But hey it's still an iteration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With