Solved

Seeking an explanation of how a "ReadOnlyCollection" works in this given case?

Posted on 2012-03-28
2
295 Views
Last Modified: 2012-03-28
I'm trying to find out what is happening in the following lines and I'm new to the "ReadyOnlyCollection." I was hoping someone could explain it to me. I'm especially confused about the line:

_children = new ReadOnlyCollection<Node>(_intChildren);
public Node Parent { get; }
private List<Node> _intChildren;
private ReadOnlyCollection<Node> _children;
public ReadOnly Collection<Node> Children
{
   get 
   {
      if (_children == null)
         _children = new ReadOnlyCollection<Node>(_intChildren);
      return _children;
   }
}

Open in new window

0
Comment
Question by:Eindoofus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37779658
A ReadOnlyCollection is just a wrapper around an existing collection. The thing that makes it "read only" is the fact that it does not expose an Add nor a Remove method. This, however, is just a facade, because if some piece of code already had access to the underlying list, then it could easily add to or remove from the collection.

This line:

_children = new ReadOnlyCollection<Node>(_intChildren);

Open in new window


is just initializing the private member variable _children with a new instance of a ReadOnlyCollection--one which wraps the private member variable _intChildren. Other parts of the class containing this code would be free to modify the underlying list (in this case:  _intChildren) since it is a member variable and thus scoped to the entire class. Outside code, however, cannot modify the list because it is private and the only public interaction with this list is by way of the Children property, which points to the ReadOnlyCollection wrapper.
0
 

Author Closing Comment

by:Eindoofus
ID: 37779764
Thanks :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question