Is there anything wrong with defining something like this:
class ObjectA
{
property a;
property b;
List <ObjectA> c;
...
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, and because the answer needs at least 30 characters, I’ll add that this is a common pattern.
Since you included the
ooptag, though, I’ll add that this pattern gives a lot of control to the outside world. Ifcis a list of children, for example, you’re giving everyone who has access to an instance of ObjectA the ability to add, delete, or replace its children.A tighter approach would be to use some sort of read-only type (perhaps implementing
IList<ObjectA>) to expose the children.EDIT
Note that the following still allows others to modify your list:
The absence of a setter only prevents outsiders from replacing the list object.