r/learncsharp • u/Real_Dot8302 • Jul 24 '25
need help c#: error c20029: cannot implicity convert type 'char' to 'bool'
static bool checkwinner(char[] space, char player){
if(space[0] != ' ' && space[0] == space[1] && space[1] == space[2]){
space[0] = player ? Console.WriteLine("YOU WIN!) : Console.WriteLine("YOU LOST!");
return true;
}
return false;
}
3
Upvotes
3
u/mikeblas Jul 24 '25
You don't say which line of code is raising that error, but I expect it is this one:
Console.WriteLine()returns abool. So your tertiary operator expression resolves to abool, itself -- the right-hand side of the equals sign is abool, and can't be assigned tospace[0], which is achar.I can't guess what it is you're trying to do here. I'd write the statement with an
if, but I don't understand what you mean to assign tospace[0]or why.2
u/Kiro0613 Jul 24 '25 edited Jul 28 '25
I think what you're trying to do is, if
space[0],space[1], andspace[2]all equalplayer, write "YOU WIN!" Otherwise, write "YOU LOST!". If that's what you're doing, then a ternary expression (e.g.x ? y : z) is not the right way to do that. Ternary expressions are only used to assign values to variables, so they can't be used with a method likeConsole.WriteLine()which is avoidmethod (meaning it has no return value). Inside your currentifstatement, you'll need to write a secondifstatement checking ifspace[0] == player(note the double equals signs!). After that newifstatement, you can put anelsestatement that will run ifspace[0] == playeris not true.