1

I'm not sure if the title is clear so, I'm declaring an array in class, but the problem is that when I want to create a class the size of array might be different and by that I want to tell the size of array to the class by the constructor. Example.

    const short DeckSize;
    private char[,] Deck = new char[DeckSize, 2];
    public HandDeck(short Size)
    {
        DeckSize = Size;
    }

So the problem here is that array has to be a constant or static as my compiler says.

I found something about readonly attribute and tried to change code to this

readonly short DeckSize;
private char[,] Deck = new char[DeckSize, 2];

public HandDeck(short Size)
{
     DeckSize = Size;
}

And it works (probably), but now the declaration doesn't meet the requirements. And at this point I am out of ideas. Any great suggestions here?

4

1 回答 1

3
    private readonly short DeckSize;
    private readonly char[,] Deck;
    public HandDeck(short Size)
    {
        DeckSize = Size;
        Deck  = new char[DeckSize, 2];
    }
于 2013-06-05T00:10:00.693 回答