9

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

4
  • Why do you want to do this without foreach? Commented Mar 11, 2013 at 23:08
  • Heres an hint : retrieve the length and iterate from 0 to length -1 Commented Mar 11, 2013 at 23:09
  • 3
    There are dozens of ways to do this. What are the requirements and why does foreach not satisfy them? Commented Mar 11, 2013 at 23:10
  • duplicate of stackoverflow.com/questions/559415/… Commented Mar 11, 2013 at 23:11

7 Answers 7

22

You can use String.Join to concatenate all lines:

string lines = string.Join(Environment.NewLine, list);
Console.Write(lines);
Sign up to request clarification or add additional context in comments.

Comments

11

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]);
}

1 Comment

Ok, For me this is the clearest version and I like it the most. Thanks
3
for (int i = 0; i < list.Count; i++)
    {
    Console.WriteLine(list[i])
    }

Comments

3

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.

2 Comments

Note that List<T>.ForEach is not a LINQ method, but just a plain old regular List<T> method; LINQ methods are never purely called for their side effects as List<T>.ForEach is.
Yeah technically I agree but these features are usually lumped together with linq :-)
1

You can use List<T>.ForEach method, which actually is not part of LINQ, but looks like it was:

list.ForEach(i => Console.WriteLine(i));

Comments

1

Well, you could try the following:

Debug.WriteLine("The folowing fruits are available:");
list.ForEach(f => Debug.WriteLine(f));

It's the very equivalent of a foreach loop, but not using the foreach keyword,

That being said, I don't know why you'd want to avoid a foreach loop when iterating over a list of objects.

Comments

1

There are three ways to iterate a List:

//1 METHOD
foreach (var item in myList)
{
    Console.WriteLine("Id is {0}, and description is {1}", item.id, item.description);
}

//2 METHOD   
for (int i = 0; i<myList.Count; i++)
{ 
    Console.WriteLine("Id is {0}, and description is {1}", myList[i].id, myMoney[i].description);
}

//3 METHOD lamda style
myList.ForEach(item => Console.WriteLine("id is {0}, and description is {1}", item.id, item.description));

1 Comment

The brevity of method 3 is popular with some devs but it has the disadvantage that you cannot "break" -out of it, if processing can be curtailed before all items have been accessed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.