Jayware33
Jayware33

Reputation: 5

Why is this string array empty?

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" }));
        }
    }

Upvotes: 0

Views: 50

Answers (1)

Tanveer Badar
Tanveer Badar

Reputation: 5523

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" }));

Upvotes: 3

Related Questions