Currently what you are doing is:
- Creating a new array.
- Storing string elements int the array.
- Then you are doing 2 things:
- Clearing the console.
- Writing the array object to the console (not its elements).
You are repeating point 3 for the number of times equal to the number of elements in your array. So if you have 5 elements, you are just clearing the console and then writing the array object to the console 5 times.
A better way would be the following:
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
string[] values = { "This", "That", "The Other Thing" };
Console.Clear();
foreach (var item in values)
{
Console.WriteLine(item);
}
This would clear the console and then write each string element contained in the array, to the console window, one element per line.
It's worth pointing out that in general, the foreach
loop is more expensive memory-wise, compared to the for
loop (see here for details). To write this using a for
loop, you could do the following:
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
string[] values = { "This", "That", "The Other Thing" };
Console.Clear();
for (int i = 0; i < values.Length; i++)
{
Console.WriteLine(values[i]);
}
The integer 'i' determines which element will be printed, and so with each cycle through the loop, the next element will be sent to the console.