When defining classes I expose class members as properties along the lines of :
class ClassA
{
private String _Name;
public String Name
{
get { return _Name; }
set { _Name = value; }
}
}
What is best practice for dealing with collections within classes, with respect to accessors
So if the class is extended to something like :
class ClassA
{
private String _Name;
private List<String> _Parts = new List<String>();
public String Name
{
get { return _Name; }
set { _Name = value; }
}
}
How do I expose the next item?
Expose a read-only instance of the collection. Note that the contents are not read-only, but the reference is.
public IList<String> Parts { get; private set; }
The naming conventions I've come across recommend
private String _name;
Also you could use automatic properties which generate the same code you've written
public string Name {get; set;}
For collections, I don't like to expose the actual collection but methods to work on it.
public void Add(...
public void Remove(...
Otherwise you could make it readonly with an automatic property
public IList<string> Parts {get; private set;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With