Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.

like image 391
Francois Avatar asked Jun 23 '26 11:06

Francois


2 Answers

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
like image 195
Yeldar Kurmangaliyev Avatar answered Jun 26 '26 00:06

Yeldar Kurmangaliyev


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!

like image 29
Sean Stayns Avatar answered Jun 26 '26 00:06

Sean Stayns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!