Let’s say I have a property:
public Collection<T> GameCollection
{
get { return new Collection<T>(_myGameList); }
}
Would this method creates new Collection object every time it’s called?
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.
Yes, it would, because of the
new. However it does not duplicatemyGameListor its contents; it only makes new wrappers for the samemyGameList(seeCollection(IList<T>)constructor).If you want to prevent any of that and return a single collection, you can initialize a backing field instead and have your getter get that field (assuming
_myGameListis already initialized too):