Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-28
2
Medium Priority
?
297 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

704 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