Search code examples
c#.netoverloadingconsole.writeline

having trouble using the console.writeline overload function to pass arrays


beginner here :

I'm I misunderstanding the use of the overloaded function?

namespace Grades
{
    class Program
    {
        private static object mike;
        private static object gus;
        private static object julio;

        static void Main(string[] args)
        {
            object[] names = new object[3];
            names[1] = mike;
            names[0] = gus;
            names[2] = julio;

            WriteAnswer(" you're cool ",  names );
        }

        static void WriteAnswer(string description, params  object[] result)
        {
            Console.WriteLine(description + " " + result);   
        }
    }
}

The output:

you're cool System.Object[]

I was under the impression that the output would be:

you're cool gus mike julio 

Or:

you're cool gus 
you're cool mike 
you're cool julio

Solution

  • First off, the problem you're experiencing has nothing to do with the overloaded function of Console.WriteLine.

    Rather.. you are creating object variables but not giving them any values to hold, so they're null.. so with your current code... it would end up with "you're cool ", and that's it because the variables that are named mike, gus, and julio don't actually contain any values.


    Solution One

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Program
    {
        private static object mike = "mike"; // give the variable a value to hold
        private static object gus = "gus"; // give the variable a value to hold
        private static object julio = "julio"; // give the variable a value to hold
        public static void Main()
        {
            object[] names = new object[3];
            names[1] = mike;
            names[0] = gus;
            names[2] = julio;
            WriteAnswer(" you're cool ", names);
        }
    
        static void WriteAnswer(string description, object[] result)
        {
                Console.WriteLine(description + string.Join(",", result));
        }
    }
    

    I have edited your code in DotNetFiddle


    Solution 2

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Program
    {
        private static object mike = "mike"; // give the variable a value to hold
        private static object gus = "gus"; // give the variable a value to hold
        private static object julio = "julio"; // give the variable a value to hold
        public static void Main()
        {
            object[] names = new object[3];
            names[1] = mike;
            names[0] = gus;
            names[2] = julio;
            WriteAnswer(" you're cool ", names);
        }
    
        static void WriteAnswer(string description, object[] result)
        {
            for(int i = 0; i < result.Length; i++)
            {
                Console.WriteLine(description + result[i]);
            }
    
        }
    }
    

    DotNetFiddle.