-2

I'm trying to write a very basic program to return the value of 2 elements in an array of string but the output is System.String[] instead of the actual value of the string. Is it because I have it two letters associated to 1 element. For example :

 static void Main(string[] args)
    {
        string[] a = new string[2] { "ugly string", "lovely string" };
        foreach (var item in a)
        {
            Console.WriteLine(a);
        }
    }

It should return ugly string and lovely string in two separate lines. However it's just showing System.String[] in 2 different lines.

3
  • 4
    Console.WriteLine(item), not Console.WriteLine(a) Commented May 11, 2021 at 14:08
  • 1
    I don't think this is a typo, but rather a genuine lack of knowledge of the correct usage of control structures, even if it's trivial to fix. Voted to reopen. Commented May 11, 2021 at 14:13
  • This is probably a duplicate of stackoverflow.com/questions/12146723/… Commented May 11, 2021 at 14:17

1 Answer 1

1

You need to change your line

Console.WriteLine(a);

to

Console.WriteLine(item);

foreach will make sure the for each (hence the name) iteration through the list called a the current value will be put into the variable named item.

Sign up to request clarification or add additional context in comments.

2 Comments

trying to learn the basics. Thank you. It worked.
@AkshayKhard think about what "a" and "item" represent in your code

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.