8

I'm trying to return an array of strings but return test; only gives "System.String[]" as output. If I iterate through test[] all values are filled in correctly. What am I doing wrong?

 public static String[] ReturnTheStrings()
 {
     FillTheArray();
     String[] test = new String[178];

     for (int i = 0; i < 178; i++)
     {
         if ((Class7.Method4(strings[i])) == "decoding wrong")
         {
             test[i] = strings[i+1];
             //System.Console.WriteLine("Non-encoded value");
         }
         else
         {
             test[i] = Class7.Method4(strings[i+1]);
             //System.Console.WriteLine("Encoded value");
         }
     }
     return test;
 }

I'm using MS Visual C# 2010.

2
  • 3
    Your code will fail with 'Index out of range' exception on the last loop. Commented Aug 27, 2012 at 17:41
  • 1
    It's worth noting that you are already returning a string array. There is nothing wrong at all with how you're returning it. Your error is in how it's used by whatever calls this method. Commented Aug 27, 2012 at 17:46

1 Answer 1

22

Calling .ToString() on an array will return "System.String[]". If you want to display each value in the array, you have iterate over it.

For example:

foreach (var value in test)
{
    Console.WriteLine(value);
}

Or, as @Oded pointed out in the comments, you can use String.Join:

Console.WriteLine(String.Join(Environment.NewLine, stringArray));
Sign up to request clarification or add additional context in comments.

Comments

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.