Francois
Francois

Reputation: 159

A Console.WriteLine("text" + function(parameter)); don't work but this instruction split into two Console.Write work fine

I started to use C# a few days ago within an algorithm course. I have a homework and there is something I need explanation for. I need to create a hangman game with three levels, each level has four words to be guessed.

What I want to print on the screen is:

Level 1 : oooo
Level 2 : oooo
Level 3 : oooo

The "emtpy circle" will be filled each time a word is found within a level.

To render this, I decided to create a char[ ].

Here is the function I am using:

public static char[] DisplayEmptyCircles(List<string> listLevel)
{
    char[] emptyCircle = new char[listLevel.Count];
    for (int counterCircles = 0; counterCircles < listLevel.Count; counterCircles++)
    {
        emptyCircle[counterCircles] = Convert.ToChar(9675);             
    }
    return emptyCircle;
}

When I use these two separate instructions everything goes well:

Console.Write("Level 1 : ");
Console.Write(DisplayEmptyCircles(firstLevel));

But I first used this one and got a System.Char[ ] printed instead of the empty circles :

Console.WriteLine("Level 1 : " + DisplayEmptyCircles(firstLevel));

Can someone tell me why?

I am sorry I know the question was asked already but I know barely nothing about C#. It's just to start learning and understanding.

Upvotes: 3

Views: 292

Answers (3)

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34234

Your DisplayEmptyCircles returns an array of char, so it cannot be concatenated to a string. You can either make your function return a string or convert its result to string when you call Console.Write.

If you just want to create a string containing N circles, then it can be achieved in a simpler way:

public static string DisplayEmptyCircles(int count)
{
    return new string((char)9675, count);
}

Console.WriteLine("Level 1 : " + DisplayEmptyCircles(firstLevel.Count));

Some more changes in the code:

  • It doesn't use char[] and utilizes String(char c, int count) constuctor
  • DisplayEmptyCircles doesn't actually need information about levels, it only needs the count
  • You don't need to use Convert.ToChar, because it just does some additional checks, which are redundant when you have a constant value

Upvotes: 4

Sean Stayns
Sean Stayns

Reputation: 4244

The Console.WriteLine converts your char array to a string. But it can't convert the combination of string + char array. So you have to convert the char array to a string. You can do it in your static function or on the fly.

In your static function:

public static string DisplayEmptyCircles(List<string> listLevel)
{
    char[] emptyCircle = new char[listLevel.Count];
    for (int counterCircles = 0; counterCircles < listLevel.Count; counterCircles++)
    {
        emptyCircle[counterCircles] = Convert.ToChar(9675);             
    }
    return new string(emptyCircle);
}

On the fly:

Console.WriteLine("Level 1 : " + new string(DisplayEmptyCircles(firstLevel)));

UPDATE:

If you want to convert the char[] in the static function, you should use the approach from Yeldar Kurmangaliyev!

Upvotes: 3

Pablo notPicasso
Pablo notPicasso

Reputation: 3161

This is because Console.Write() will use char[] as input and in Console.WriteLine(...) you will end up with converting char[] to string with .ToString() method. Use new string( DisplayEmptyCircles(firstLevel))

Console.WriteLine("Level 1 : " + new string(DisplayEmptyCircles(firstLevel)));

Upvotes: 2

Related Questions