public abstract class Token
{
private List<Token> _tokens { get; set; }
public IEnumerable<Token> Tokens
{
get { return _tokens; }
}
protected Token AddToken (Token token)
{
_tokens.Add(token);
return token;
}
protected Token GetTokenAt(int index)
{
return _tokens[index];
}
}
I don't like returning read-only collections, because IEnumerable
is a read-only interface which hides implementation from consumer. Some can argue, that consumer may cast IEnumerable
to list and add new items, but that means two things:
- consumer violates API you provided
- you can't stop consumer from reflection usage