Passing ReadOnlyCollection using the AsReadonly method so that user cannot modify the list

Hi,

This was new to me so as always I decided to share with all. The generic List has a method AsReadonly. The methods can be used if you want to pass the list as readonly. The return value of the method is a ReadOnlyCollection.

The ReadOnlyCollection acts as a wrapper around the list. The only difference of a readonlycollection and a list is that a readonlycollection has a wrapper around it which prevents the modification of the collection.

If there are changes made to the underlying collection, the changes will be refelected in the readonly collection.

Let’s say you have a private collection (list) and want to expose it as read only format. This is when this method will be useful.

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia