0

Cant figure out why this isnt working? When I console.writeline the values inside the loop, newFruits contains the correct values. All I get is System.String[] as the output

 public static string[] FuncSortItemsInList(string[] fruits)
        {
            Array.Sort(fruits);
            Array.Reverse(fruits);
            string[] newFruits = new string[fruits.Length];
            for (int i = 0; i< fruits.Length; i++)
            {              
                newFruits[i] = fruits[i];              
            }
            return newFruits; 
        }
    }
    class Program
    {
        static void Main()
        {
            //Food.SortItemsInList(new string[] { "Banana", "Apple", "Pineapple" }); 
            Console.WriteLine(Food.FuncSortItemsInList(new string[] { "Banana", "Apple", "Pineapple" }));
        }
    }
1
  • if you are using an IDE like visuals tudio, paying attention to the popup that shows the function signature might help. Console.WriteLine accepts and prints a single string among other overloads, but not an array of strings in any case. Commented Nov 24, 2019 at 13:25

1 Answer 1

3

It is not empty, Console.WriteLine(object) overload is being called in this case since you did not specify any format string. That overload calls ToString() on the object passed in, which in your case is a string[].

To get the intended output, you need to write code like this

 Console.WriteLine(string.Join(", ", Food.FuncSortItemsInList(new string[] { "Banana", "Apple", "Pineapple" }));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - is it possible to have the items displayed on a line-by-line basis as opposed to joined together?
It is definitely possible. But look for that in the documentation itself. See details for string.Join() and how to represent newlines in strings. SO is not a code-writing service. There are multiple ways to output string array in different lines, you are better off learning those on your own by looking at documentation.
@Jayware33 replace ", " with Environment.NewLine in the string.Join

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.