It depends on how serious you are about how to encapsulate the way you store data. If you make an easy class, and you just provide storage, but want to completely leave the decision on access to the consumer of your class, you simply expose it as a standard property or create its auto-property.
public List<String> Parts { get; private set; }
If you want the variable to never be null, continue to use your private support field and add checks.
private List<String> _Parts; public IList<String> Parts { get { if (_Parts == null) _Parts = new List<String>(); return _Parts; } private set { if (value != null) _Parts = value; } }
If you want to control synchronization or something like that, you would expose the logical methods for what you are doing.
public void AddPart(String part); public void RemovePart(String part); public String GetPart(int index); public IEnumerable<String> GetAllParts() { foreach(String part in _Parts) yield return part; }
source share