I have following class, and wondering if declaring ListOfItems
as public with a private setter is a proper way (from the OOP best practice point of view).
public abstract class Game
{
public List<int> ListOfItemss { get; private set; }
protected Game()
{
}
protected Game(string gameInput)
{
ListOfItems = new List<int>();
var gameParser = new GameParser();
ListOfItems = gameParser.ParseGameInputString(gameInput);
}
public abstract int Bowl(int items);
}
Or should I made it via private field i.e.
public abstract class Game
{
private List<int> _listOfItems;
public List<int> ListOfItemss
{
get { return _listOfItems; }
private set { _listOfItems = value; }
}
protected Game()
{
}
protected Game(string gameInput)
{
ListOfItems = new List<int>();
_listOfItems = new List<int>();
var gameParser = new GameParser();
_listOfItems = gameParser.ParseGameInputString(gameInput);
}
public abstract int Bowl(int items);
}
-
-
Updated based on the suggestions: Based on the suggestions below, here is the code I'm think to have in the final version:
public abstract class Game
{
public IReadOnlyCollection<int> ListOfItems { get; } = new List<int>();
protected Game()
{
}
protected Game(string gameInput)
{
var gameParser = new GameParser();
ListOfItems = gameParser.ParseGameInputString(gameInput);
}
public abstract int Bowl(int items);
}