-3

I have a enum class "Figure" and board:

public enum Figure { X, O }

private Figure[,] board = new Figure[boardSize, boardSize];

When there are no values on the board, I want add null value to board. Example: board[0,0] = null - but it's wrong. How i can do that?

IngBond
  • 601
  • 1
  • 5
  • 18
  • Please read [ask]. "it's wrong" is not a proper problem description. If you research the actual compiler error, _"Cannot convert null to enum because it is a value type"_, you'll find plenty of results. – CodeCaster Sep 09 '17 at 08:34

1 Answers1

5

Enums are value types, so null is not a valid value for them.

Here's two solutions to this problem:

  1. add a new enum value:

    public enum Figure { Empty, X, O }
    

You can now use Figure.Empty instead of null.

  1. Use a nullable type:

    private Figure?[,] board = new Figure[boardSize, boardSize];
    

The little ? after Figure allows you to use null.

Sweeper
  • 213,210
  • 22
  • 193
  • 313