Solved

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

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dot Net 4.6.1 installation fails on W2012 R2 server 14 42
C# MVC Insert Multiple Row into DB 2 32
jquery + C#.net radiobuttonlist 3 30
abstract class C# 1 31
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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