Solved

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

Posted on 2012-03-28
2
293 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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to prevent website from publishing with errors 9 47
SSRS subreport with parameter 3 33
VB.NET - Error - Object not set in Module 4 33
C# LINQ ForEach() question 6 28
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 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