Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this program, I have a foreach loop that will print out the array. However, I just want to take the last loop as the result that run thorugh the foreach loop.

Current result showed:
h,
h, e,
h, e, l,
h, e, l, l,
h, e, l, l, o,


What should I try if I only want the result show the last one which is h, e, l, l, o ?

What I have tried:

char[] myList = { 'h', 'e', 'l', 'l', 'o' };
string list = string.Empty;
foreach (char c in myList)
{
    //Console.WriteLine(c);
    list += c + ", ";
    Console.WriteLine(list);
}
Posted
Updated 5-Jul-22 4:32am

Move the WriteLine outside the loop:
C#
char[] myList = { 'h', 'e', 'l', 'l', 'o' };
string list = string.Empty;
foreach (char c in myList)
{
    list += c + ", ";
}
Console.WriteLine(list);
 
Share this answer
 
Comments
CPallini 5-Jul-22 7:40am    
5.
Richard MacCutchan 5-Jul-22 9:34am    
Thank you, the most challenging problem today. :)
You need nested for loops: an outer one which prints each row running from 0 to the array Length minus one as y, and an inner one which runs from 0 to y as x.
Inside the inner array, print each character with Console.Write using x as an index into the array.
After each time the inner loop finishes, print a new line.

Think about it for a couple of minutes, and you'll get the idea.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

There is no need for a foreach loop. Just create a new string.

C#
char[] myList = { 'h', 'e', 'l', 'l', 'o' };
Console.WriteLine(new string(myList));

 
Share this answer
 
Comments
Richard MacCutchan 5-Jul-22 11:03am    
It is after all a rather pointless exercise. :)
George Swan 5-Jul-22 11:54am    
Indeed it is. I suspect that the OP did not know that the string class had a constructor that took a character array.
Dave Kreskowiak 5-Jul-22 13:50pm    
Or this is a homework assignment covering foreach...
George Swan 5-Jul-22 13:57pm    
A good point, Dave. I never thought of that, you could well be right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900