Read-Only Lists in Visual C#.NET

Maker Data Matrix in Visual C#.NET Read-Only Lists

Read-Only Lists
ECC200 Generation In C#
Using Barcode creation for Visual Studio .NET Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
DataMatrix Recognizer In Visual C#.NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Read-only collections are useful when you have collected all related data items and need to ensure that this set of items cannot be modified. If you are working with the List<T> class, there is built-in support for creating a read-only version of the collection, as shown by Listing 19-34. Listing 19-34. Creating a Read-Only List<T> using System; using System.Collections.Generic; namespace Listing 34 { class Listing 34 { static void Main(string[] args) { // create a normal List<T> and populate it List<string> normalList = new List<string>() { "apple", "orange", "banana" }; // create a readonly list from the normal list IList<string> readonlyList = normalList.AsReadOnly(); // report on Read-Only state of each list Console.WriteLine("normalList is read-only: {0}", ((IList<string>)normalList).IsReadOnly); Console.WriteLine("readonlyList is read-only: {0}", readonlyList.IsReadOnly); // try to modify the read-only collection Console.WriteLine("\nTrying to remove an item from the read-only list"); try { readonlyList.Add("guava"); } catch (Exception ex) { Console.WriteLine("Exception: {0}", ex.GetType()); } // remove an item from the normal List<T> normalList.Remove("orange"); // enumerate the contents of the read-only list Console.WriteLine("\nEnumerating readonly list");
Encoding Matrix 2D Barcode In C#
Using Barcode generator for Visual Studio .NET Control to generate, create 2D image in .NET applications.
www.OnBarcode.com
PDF 417 Drawer In C#
Using Barcode creator for .NET framework Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
CHAPTER 19 COLLECTIONS
Create Universal Product Code Version A In Visual C#
Using Barcode creation for Visual Studio .NET Control to generate, create UPC-A Supplement 2 image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Drawer In Visual C#
Using Barcode printer for .NET Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
foreach (string s in readonlyList) { Console.WriteLine("Readonly List Item: {0}", s); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } } In the example, I create a regular List<T> and populate it with three string items. I then call the AsReadOnly method to create a read-only version of the collection. I assign the result of calling the AsReadOnly method to an IList<string>, but the underlying class that is returned is System.Collections.ObjectModel.ReadOnlyCollection, which also implements IList<T>, ICollection<T>, and IEnumerable<T> interfaces. You can determine whether a collection is read-only by using the IsReadOnly property of the ICollection<T> interface. In the example, I have written out the result of this property for both of the lists. Using a read-only IList<T> is just like using a regular list, unless you try to modify the contents, in which case an instance of System.NotSupportedException is thrown. You can see this in the listing when I try to add guava to the collection. Compiling and running the code in Listing 19-34 produces the following results: normalList is read-only: False readonlyList is read-only: True Trying to remove an item from the read-only list Exception: System.NotSupportedException Enumerating readonly list Readonly List Item: apple Readonly List Item: banana Press enter to finish Toward the end of the listing, I remove an item from the regular list and then enumerate the readonly list. You can see from the results that the item I removed is gone from both lists. As you can see, some care if required to ensure that you don t pass around references to the regular list since it can be used to modify both collections and so undermine the purpose of using a read-only collection in the first place. If you want to disassociate the read-only list from the regular list, then you can create an intermediate collection that will be used by the read-only list as follows:
EAN / UCC - 13 Encoder In C#.NET
Using Barcode generator for VS .NET Control to generate, create GS1 - 13 image in .NET applications.
www.OnBarcode.com
4-State Customer Barcode Drawer In C#.NET
Using Barcode creator for Visual Studio .NET Control to generate, create OneCode image in Visual Studio .NET applications.
www.OnBarcode.com
IList<string> readonlyList = new List<string>(normalList).AsReadOnly();
Generating DataMatrix In VB.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in VS .NET applications.
www.OnBarcode.com
Draw ECC200 In Java
Using Barcode maker for Android Control to generate, create ECC200 image in Android applications.
www.OnBarcode.com
Other Read-Only Collections
PDF-417 2d Barcode Drawer In None
Using Barcode encoder for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com
Making ECC200 In Java
Using Barcode generation for BIRT Control to generate, create Data Matrix 2d barcode image in BIRT applications.
www.OnBarcode.com
Although List<T> is the only collection class that includes built-in support for creating read-only instances, you can achieve the same effect by creating an implementation of one of the collection interfaces and writing a wrapper around a regular collection instance. Methods that you want to support are mapped to the underlying collection, while the ones that you don t want to support throw a
Draw EAN-13 In None
Using Barcode creator for Office Word Control to generate, create EAN / UCC - 13 image in Microsoft Word applications.
www.OnBarcode.com
Print EAN / UCC - 13 In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications.
www.OnBarcode.com
CHAPTER 19 COLLECTIONS
PDF417 Printer In None
Using Barcode generator for Office Excel Control to generate, create PDF-417 2d barcode image in Office Excel applications.
www.OnBarcode.com
Data Matrix ECC200 Encoder In None
Using Barcode generator for Online Control to generate, create Data Matrix 2d barcode image in Online applications.
www.OnBarcode.com
NotSupportedException. Listing 19-35 demonstrates creating such a read-only wrapper for Dictionary<TKey, TVal>. Listing 19-35. Creating a Read-Only Collection using System; using System.Collections.Generic; namespace Listing 35 { class Listing 35 { static void Main(string[] args) { // create a regular dictionary Dictionary<string, string> dict = new Dictionary<string, string>() { {"apple", "green"}, {"orange", "orange"}, {"cherry", "red"}}; // create the read-only wrapper ReadOnlyDictionary<string, string> readOnlyDict = new ReadOnlyDictionary<string,string>(dict); // enumerate the keys foreach (string s in readOnlyDict.Keys) { Console.WriteLine("Key: {0}", s); } // try to add an item to the read-only collection try { readOnlyDict.Add("pear", "green"); } catch (Exception ex) { Console.WriteLine("Exception: {0}", ex.GetType()); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } class ReadOnlyDictionary<TKey, TVal> : IDictionary<TKey, TVal> { IDictionary<TKey, TVal> readwriteDict; public ReadOnlyDictionary(IDictionary<TKey, TVal> rwdict) { readwriteDict = rwdict; } public void Add(TKey key, TVal value) { throw new NotSupportedException(); } public bool ContainsKey(TKey key) {
Decoding Barcode In .NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Code39 Generation In VB.NET
Using Barcode maker for .NET Control to generate, create Code 39 Full ASCII image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Generator In Objective-C
Using Barcode generator for iPad Control to generate, create Barcode image in iPad applications.
www.OnBarcode.com
Make Barcode In None
Using Barcode creator for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Copyright © OnBarcode.com . All rights reserved.