0

I'm trying to get the contents of an array to display but all that happens is this prompt

enter image description here

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] values = { "This", "That", "The Other Thing" };
            foreach (var item in values)
            {
                Console.Clear();
                Console.WriteLine(values);
            }

What am I doing wrong?

klashar
  • 2,519
  • 2
  • 28
  • 38
Justin
  • 357
  • 2
  • 7
  • 18

5 Answers5

4

Print item, not values:

// Clear once, not before each output 
Console.Clear();

// print item, not entire array 
foreach (var item in values)
  Console.WriteLine(item);

In case you want to print the entire array in one go, do it with a help of string.Join:

Console.Clear();
Console.Write(string.Join(Environment.NewLine, values));    
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
2

Right now what you are printing is the array itself, when you want to print the items in the array.

namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] values = { "This", "That", "The Other Thing" };
                foreach (var item in values)
                {
                    Console.Clear();
                    Console.WriteLine(item);
                }
Rick james
  • 824
  • 1
  • 11
  • 30
1

Little help to dig deeper on dissecting what we are doing:

object[] values = { "This", "That", "The Other Thing", 1, 2, 1.0f };
foreach (var item in values)
{
    Console.WriteLine($"I am printing `{item}` from `{values}` which is of type `{item.GetType()}`");
}

The output:

enter image description here

L J
  • 5,249
  • 3
  • 19
  • 29
1

Currently what you are doing is:

  1. Creating a new array.
  2. Storing string elements int the array.
  3. 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.

JMadelaine
  • 2,859
  • 1
  • 12
  • 17
  • Hey JMadel, thank you very much for your thorough answer! I really appreciate all the information as well as the link. Thanks again – Justin Feb 18 '17 at 00:04
0
   foreach (var item in values)
        {

            Console.WriteLine(item );
        }
Ashkan S
  • 10,464
  • 6
  • 51
  • 80