Solved

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

Posted on 2012-03-28
2
287 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
2 Comments
 
LVL 74

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now