Collections


Arrays are the most popular collection. However, the .NET FCL offers a variety of other collections with different semantics. Collections are abstractions of data algorithms. ArrayList is an abstraction of a dynamic array, the Stack collection abstracts a stack data structure, the Queue collection abstracts queues, the Hashtable collection abstracts a lookup table, and so on. Each collection exposes both unique and standard interfaces. The unique interface is specific to the collection type. For example, the Stack type has pop and push methods, whereas the Queue type has Dequeue and Enqueue methods. Collections minimally implement the ICollection, IEnumerable, and ICloneable interfaces. (These interfaces were described earlier in this chapter.) The nongeneric collection classes are implemented in the System.Collections namespace. Generic collections are reviewed in Chapter 6, "Generics."

Table 5-5 lists the nongeneric collections in the .NET FCL.

Table 5-5: Collection Types

Class Name

Description

ArrayList

Dynamic array

BitArray

Bit array

Hashtable

Lookup table of keys and values

Queue

First-in/first-out (FIFO) collection of elements

SortedList

Sorted list of elements

Stack

Last-in/first-out (LIFO) collection of elements

What follows is a detailed explanation of each collection found in the Collections namespace. The explanations are complemented with sample code, which demonstrates the uniqueness of each collection.

ArrayList Collection

An array list is a dynamic array. Although indigenous arrays are static, elements can be added or removed from an ArrayList at run time. Elements of the ArrayList are not automatically sorted. Similar to single-dimensional arrays, the elements of an ArrayList are accessible using the indexing operator and indices.

In addition to the standard collection interfaces, ArrayList implements the IList interface.

Table 5-6 lists the ArrayList-specific methods and properties. The static members of ArrayList are thread-safe, whereas instance members are not. The common collection interfaces—ICollection, IEnumerable, and ICloneable—are not discussed in detail. (These interfaces and their members were reviewed earlier in the chapter.)

Table 5-6: ArrayList Members

Member Name

Syntax

Constructors

 ArrayList() ArrayList(     ICollection sourceCollection) ArrayList(int capacity) 

Adapter

This method creates a wrapper for an IList collection.

 static ArrayList Adapter(     IList list) 

Add

This method adds an element to the end of the ArrayList collection.

 virtual int Add(object value) 

AddRange

This method adds a range of elements to the ArrayList collection. The elements are input from an ICollection type, such as a regular array.

 virtual void AddRange(     ICollection elements) 

BinarySearch

This method performs a binary search for a specific value in a sorted array.

 virtual int BinarySearch(     object value) virtual int BinarySearch(     object value,     IComparer comparer) virtual int BinarySearch(     int index,     int count,     object value,     IComparer comparer) 

Capacity

This property gets or sets the number of properties allowed in the collection. The default capacity is 16 elements. Capacity is different from the number of elements. The capacity is automatically increased as elements are added. When the number of elements exceeds the current capacity, the capacity doubles. Capacity is for better memory management of elements in the collection.

 virtual int Capacity { get; set} 

Clear

This method removes all the elements of the collection.

 virtual void Clear() 

Contains

This method returns true if the specified item is found in a collection. If the item is not found, false is returned.

 virtual bool Contains(object item) 

Count

This property returns the number of elements in the collection.

 virtual int Count{     get; } 

FixedSize

This method creates a wrapper for an ArrayList or IList collection, in which elements cannot be added or removed.

 static ArrayList FixedSize(     ArrayList sourceArray) static IList FixedSize(     IList sourceList) 

GetRange

This method returns a span of elements from the current array. The result is stored in a destination ArrayList.

 virtual ArrayList GetRange(     int index, int count) 

IndexOf

This method returns the index of the first matching element in the collection.

 virtual int IndexOf(     object value) virtual int IndexOf(object value,     int startIndex) virtual int IndexOf(object value,    int startIndex,    int count) 

Insert

This method inserts an element into the collection at the specified index.

 virtual void Insert(int index,     object value) 

InsertRange

This method inserts multiple elements into the collection at the specified index.

 virtual void InsertRange(     int index,     ICollection sourceCollection) 

IsFixedSize

This property returns true if the collection is fixed-length. Otherwise, the property returns false.

 virtual bool IsFixedSize{     get;} 

IsReadOnly

This property returns true if the collection is read-only. Otherwise, the property returns false.

 virtual bool IsReadOnly{     get;} 

Item

This property gets or sets the element at the index.

 virtual object this[int index] {    get;set;} 

LastIndexOf

This method returns the index of the last matching element in the collection.

 virtual int LastIndex(     object value) virtual int LastIndexOf(object value,     int startIndex) virtual int LastIndexOf(object value,     int startIndex,     int count) 

ReadOnly

This method creates a read-only wrapper for an IList object.

 static ArrayList ReadOnly(     ArrayList sourceArray) static IList ReadOnly(     IList sourceList) 

Remove

This method removes the first element in the collection that matches the value.

 virtual void Remove(     object value) 

RemoveAt

This method removes the element at the index from the collection.

 virtual void RemoveAt(     int index) 

RemoveRange

This method removes a range of elements from a collection.

 virtual void RemoveRange(     int index,     int count) 

Repeat

This method returns an ArrayList with each element initialized to the same value. Count is the number of times to replicate the value.

 static ArrayList Repeat(     object value,     int count) 

Reverse

This method reverses the order of elements in the collection.

 virtual void Reverse() virtual void Reverse(     int beginIndex,     int endingIndex) 

SetRange

This method copies elements from a collection into the same elements in the ArrayList collection.

 virtual void SetRange(     int index,     ICollection sourceCollection) 

Sort

This method sorts an ArrayList.

 virtual void Sort() virtual void Sort(     IComparer comparer} virtual void Sort(int index,     int count,     IComparer comparer) 

Synchronized

This method returns a thread-safe wrapper of an ArrayList or IList object.

 static ArrayList Synchronized(     ArrayList sourceArray) static IList Synchronized(     IList sourceList) 

ToArray

This method copies elements from the current array into a new collection.

 virtual object [] ToArray() virtual Array ToArray(Type type) 

TrimToSize

This method sets the capacity to the number of elements in the collection.

 virtual void TrimToSize() 

IEnumerable members

GetEnumerator

ICloneable members

Clone

ICollection members

CopyTo, Count, IsSynchronized, and SyncRoot

The following code uses various ArrayList methods and properties. It creates a new ArrayList, which is then initialized with command-line parameters. The Add method is called to add elements to the ArrayList. The ArrayList is then sorted and cloned. Then the values at the elements of the cloned ArrayList are doubled. Afterward, the cloned ArrayList is enumerated and every element is displayed.

 using System; using System.Collections; namespace Donis.CSharpBook{     public class Starter{         public static void Main(string [] argv){             ArrayList al1=new ArrayList();             foreach(string arg in argv) {                 al1.Add(int.Parse(arg));             }             al1.Sort();             ArrayList al2=(ArrayList)al1.Clone();             for(int count=0;count<al2.Count;++count) {                 al2[count]=((int)al2[count])*2;             }             foreach(int number in al2) {                 Console.WriteLine(number);             }         }     } } 

BitArray Collection

The BitArray collection is a composite of bit values. Bit values are 1 and 0, where 1 is true and 0 false. This collection provides an efficient means of storing and retrieving bit values.

Table 5-7 list the BitArray-specific methods and properties. The static members of the BitArray are thread-safe, whereas instance members are not.

Table 5-7: BitArray Members

Member Name

Syntax

Constructor

The BitArray constructor is overloaded. These are some of the overloaded constructors.

 BitArray(bool [] bits) BitArray(int [] bits) BitArray(int count,     bool default) 

And

This method performs a bitwise And on the current and BitArray parameter. The result is placed in the returned BitArray.

 BitArray And(BitArray value) 

Get

This method returns a specific bit in the BitArray collection.

 bool Get(int index) 

IsReadOnly

This property returns true if the collection is read-only. Otherwise, the property returns false.

 virtual bool IsReadOnly{     get;} 

Item

This property gets or sets the bit at the index.

 virtual object this[int index] {     get;set;} 

Length

This property gets or sets the number of bits in the collection.

 public int Length{     get; set; } 

Not

This method negates the bits of the BitArray collection. The result is placed in the returned BitArray.

 BitArray Not() 

Or

This method performs a bitwise Or on the current and BitArray parameter. The result is placed in the returned BitArray.

 BitArray Or(BitArray value) 

Set

This method sets a specific bit in the collection.

 Void Set(int index, bool value) 

SetAll

This method sets all the bits of the collection to true or false.

 void SetAll(bool value) 

Xor

This method performs an exclusive OR on the current collection and the BitArray parameter.

 BitArray Xor(     BitArray value) 

IEnumerable members

GetEnumerator

ICloneable members

Clone

ICollection members

CopyTo, Count , IsSynchronized , and SyncRoot

The following code demonstrates the BitArray collection. The Employee class contains a BitArray collection that tracks employee enrollment in various programs, such as the health plan and credit union. This is convenient because enrollment is either true or false and never maybe. In the Employee class, properties are provided to set and get enrollment in various programs.

 using System; using System.Collections; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             Employee ben=new Employee();             ben.InProfitSharing=false;                 ben.InHealthPlan=false;                 Employee valerie=new Employee();                 valerie.InProfitSharing=false;                 Participation("Ben", ben);                 Participation("Valerie", valerie);             }             public static void Participation(string name, Employee person) {                 Console.WriteLine(name+":");                 if(person.InProfitSharing) {                     Console.WriteLine(" Participating in"+                         " Profit Sharing");                 }                 if(person.InHealthPlan) {                     Console.WriteLine(" Participating in"+                         " Health Plan");                 }                 if(person.InCreditUnion) {                     Console.WriteLine(" Participating in"+                         " Credit Union");                 }             }         }         public class Employee {             public Employee() {                 eflags.SetAll(true);             }             private BitArray eflags=new BitArray(3);             public bool InProfitSharing{                 set {                     eflags.Set(0, value);                 }                 get {                     return eflags.Get(0);                 }             }             public bool InHealthPlan{                 set {                     eflags.Set(1, value);                 }                 get {                     return eflags.Get(1);                 }             }             public bool InCreditUnion{                 set {                     eflags.Set(2, value);                 }                 get {                 return eflags.Get(2);             }         }     } } 

Hashtable Collection

The Hashtable collection is a collection of key/value pairs. Entries in this collection are instances of the DictionaryEntry type. DictionaryEntry types have a Key and Value property to get and set keys and values.

In addition to the standard collection interfaces, the Hashtable collection implements the IDictionary, ISerializable, and IDeserializationCallback interfaces.

The entries are stored and retrieved in order based on a hash code of the key.

Table 5-8 lists the members of the Hashtable collection.

Table 5-8: Hashtable Members

Member Name

Syntax

Constructor

The Hashtable constructor is overloaded. These syntaxes are some of the overloaded constructors.

 Hashtable() Hashtable(int capacity) Hashtable(int capacity,     float loadFactor) 

Add

This method adds an element to the collection.

 virtual void Add(object key,     object value) 

Contains

This method returns true if the key is found in the collection. If the key is not present, false is returned.

 virtual bool Contains(     object key) 

ContainsKey

This method returns true if the key is found in the collection. If the key is not present, false is returned. Identical to the Contains method.

 virtual bool ContainsKey(     object key) 

ContainsValue

This method returns true if the value is found in the collection. If the value is not present, false is returned.

 virtual bool ContainsValue(     object value) 

EqualityComparer

 IEqualityComparer EqualityComparer {     get;}. 

GetHash

This method returns the hash code for the specified key.

 virtual int GetHash(     object key) 

GetObjectData

This is the method implemented to serialize the collection.

 virtual void GetObjectData(     SerializationInfo info,     StreamingContext context) 

IsFixedSize

This property returns true if the collection is fixed size. Otherwise, false is returned.

 virtual bool IsFixedSize{     get;} 

IsReadOnly

This property returns true if the collection is read-only. Otherwise, false is returned.

 virtual bool IsReadOnly{     get;} 

IsSynchronized

This property returns true if the collection is synchronized.

 virtual bool IsSynchronized{     get;} 

Item

This property gets or sets a value related to a key.

 virtual object this[object key]{     get; set;} 

KeyEquals

This method compares a key to a value. If equal, true is returned. Otherwise, false is returned. This method is primarily used to compare two keys.

 virtual bool KeyEquals(     object item,     object key) 

Keys

Returns a collection that contains the keys of the Hashtable.

 virtual ICollection Keys{     get;} 

OnDeserialization

This method is called when deserialization is completed.

 virtual void OnDeserialization(     object sender) 

Remove

This method removes an element with the specified key from the collection.

 virtual void Remove(     object key) 

Synchronized

This method returns a thread-safe wrapper of the collection.

 static Hashtable Synchronized(     Hashtable sourceTable) 

Values

Returns a collection that has the values of the Hashtable.

 virtual ICollection values{     get;} 

IEnumerable members

GetEnumerator

ICloneable members

Clone

ICollection members

CopyTo, Count, IsSynchronized, and SyncRoot

Hashtable.GetEnumerator implements IDictionary.GetEnumerator, which returns an IDictionary Enumeator. IDictionaryEnumerator implements the IEnumerator interface. It also adds three properties: Entry, Key, and Value.

The following code extends the previous sample code for the BitArray collection. The program creates a Hashtable, in which employee identifiers are the keys. The values associated with the keys are instances of the Employee type.

 using System; using System.Collections; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             Hashtable employees=new Hashtable();             employees.Add("A100", new Employee(                 "Ben", true, false, true));             employees.Add("V100", new Employee(                 "Valerie", false, false, true));             Participation((Employee) employees["A100"]);             Participation((Employee) employees["V100"]);         }         public static void Participation(Employee person) {             Console.WriteLine(person.Name+":");             if(person.InProfitSharing) {                 Console.WriteLine(" Participating in"+                     " Profit Sharing");             }             if(person.InHealthPlan) {                 Console.WriteLine(" Participating in"+                     " Health Plan");             }             if(person.InCreditUnion) {                 Console.WriteLine(" Participating in"+                     " Credit Union");             }         }     }     public class Employee {         public Employee(string emplName) {             propName=emplName;             eflags.SetAll(true);         }         public Employee(string emplName,                         bool profitSharing,                         bool healthPlan,                         bool creditUnion) {             propName=emplName;             InProfitSharing=profitSharing;             InHealthPlan=healthPlan;            InCreditUnion=creditUnion;        }        private BitArray eflags=new BitArray(3);        public bool InProfitSharing{            set {                eflags.Set(0, value);            }            get {                return eflags.Get(0);            }        }        public bool InHealthPlan{            set {                eflags.Set(1, value);            }            get {                return eflags.Get(1);             }         }         public bool InCreditUnion{             set {                 eflags.Set(2, value);             }             get {                 return eflags.Get(2);             }         }         private string propName;         public string Name {             get {                 return propName;             }         }     } } 

This is sample code of the IDictionaryEnumerator enumerator:

 using System; using System.Collections; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             Hashtable zHash=new Hashtable();             zHash.Add("one", 1);             zHash.Add("two", 2);             zHash.Add("three", 3);             zHash.Add("four", 4);             IDictionaryEnumerator e=                 zHash.GetEnumerator();             while(e.MoveNext()){                 Console.WriteLine(                     "{0} {1}",                     e.Key, e.Value);             }         }     } } 

Queue Collection

Queue collections abstract FIFO data structures. The initial capacity is 32 elements. Queue collections are ideal for messaging components.

Table 5-9 lists the member of the Queue collection.

Table 5-9: Queue Members

Member Name

Syntax

Constructor

 public Queue() public Queue(     ICollection sourceCollection) public Queue(int capacity) public Queue(int capacity,     float factor) 

Clear

This method removes all the elements of the collection.

 virtual void Clear() 

Contains

This method returns true if the specified value is found in the collection. If the value is not found, false is returned.

 virtual bool Contains(object value) 

Dequeue

This method removes and returns the first element of the queue.

 virtual object Dequeue() 

Enqueue

This method adds an element to the queue.

 virtual void Enqueue(     object element) 

Peek

This method returns the first element of the queue without removing it.

 virtual object Peek() 

Synchronized

This method returns a thread-safe wrapper for a queue object.

 static Queue Synchronized(     Queue sourceQueue) 

ToArray

This method creates a new array initialized with the elements of the queue.

 virtual object[] ToArray() 

TrimToSize

This method sets the capacity to the number of elements in the collection.

 virtual void TrimToSize() 

IEnumerable members

GetEnumerator

ICloneable members

Clone

ICollection members

CopyTo, Count, IsSynchronized, and SyncRoot

This is sample code of the Queue collection. Customers are added to the queue and then displayed.

 using System; using System.Collections; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             Queue waiting=new Queue();             waiting.Enqueue(new Customer("Bob"));             waiting.Enqueue(new Customer("Ted"));             waiting.Enqueue(new Customer("Kim"));             waiting.Enqueue(new Customer("Sam"));             while(waiting.Count!= 0) {                 Customer cust=                     (Customer) waiting.Dequeue();                 Console.WriteLine(cust.Name);             }         }         public class Customer{             public Customer(string cName) {                 propName=cName;             }             private string propName;             public string Name {                 get {                     return propName;                 }             }         }     } } 

SortedList

The SortedList collection is a combination of key/value entries and an ArrayList collection, where the collection is sorted by the key. The collection is accessible per the key or an index.

Table 5-10 include the members of the SortedList collection.

Table 5-10: SortedList Members

Member Name

Syntax

Constructor

The SortedList constructor is overloaded. These are some of the overloaded constructors.

 SortedList() SortedList(IComparer comparer) SortedList(     IDictionary sourceCollection) 

Add

This method adds an element to the collection.

 virtual void Add(object key,     object value) 

Capacity

This property gets or sets the capacity of the collection.

 virtual int Capacity{     get; set;} 

Clear

This method removes all the elements of the collection.

 virtual void Clear() 

Contains

This method returns true if the specified value is found in the collection. If the value is not found, false is returned.

 virtual bool Contains(object value) 

ContainsKey

This method returns true if the key is found in the collection. If the key is not present, false is returned. Identical to the Contains method.

 virtual bool ContainsKey(     object key) 

ContainsValue

This method returns true if the value is found in the collection. If the value is not present, false is returned.

 virtual bool ContainsValue(     object value) 

GetByIndex

This method returns the value at the index.

 virtual object GetByIndex(     int index) 

GetKey

This method returns the key at the specified index.

 virtual object GetKey(     int index) 

GetKeyList

This method returns all the keys in the collection.

 virtual IList GetKeyList() 

GetValueList

This method returns all the values of the SortedList in a new collection.

 virtual IList GetValueList() 

IndexOfKey

This method returns the index of a key found in the collection.

 virtual int IndexOfKey(     object key) 

IndexOfValue

This method returns the index to the first instance of this value in the collection.

 virtual int IndexOfValue(     object value) 

IsFixedSize

This property returns true if the collection is fixed size. Otherwise, false is returned.

 virtual bool IsFixedSize{     get;} 

IsReadOnly

This property returns true if the collection is read-only. Otherwise, false is returned.

 virtual bool IsReadOnly{     get;} 

Item

This property gets or sets the value of this key.

 virtual object this[object key]     {get; set;} 

Keys

This property returns the keys of the SortedList collection.

 public virtual ICollection Keys{     get;} 

Remove

This method removes an element, which is identified by the key, from the collection.

 virtual void Remove(     object key) 

RemoveAt

This method removes an element at the specific index.

 virtual void RemoveAt(     int index) 

SetByIndex

This method set the value of the element at the specified index.

 virtual void SetByIndex(     int index, object value) 

Synchronized

This method returns a thread-safe wrapper for a queue object.

 static SortedList Synchronized(     SortedList sourceList) 

TrimToSize

This method trims the capacity to the actual number of elements in the collection.

 virtual void TrimToSize() 

Values

This property returns the values of the collection.

 virtual ICollection Values{     get;} 

IEnumerable members

GetEnumerator

ICloneable members

Clone

ICollection members

CopyTo, Count, IsSynchronized, and SyncRoot

This following program is an application that tracks auto repair tickets. Each ticket, which is an instance of the AutoRepairTicket, is added to a sorted list. The key is the customer name. The value is the actual ticket. After populating the SortedList type, the CustomerReport method lists the open tickets.

 using System; using System.Collections; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             SortedList tickets=new SortedList();             AutoRepairTicket ticket=NewTicket("Ben");             tickets.Add(ticket.Name, ticket);             ticket=NewTicket("Donis");             tickets.Add(ticket.Name, ticket);             ticket=NewTicket("Adam");             tickets.Add(ticket.Name, ticket);             CustomerReport(tickets);         }         public static AutoRepairTicket NewTicket(                 string customerName) {             return new AutoRepairTicket(customerName,                 DateTime.Now);         }         public static void CustomerReport(SortedList list) {             foreach(DictionaryEntry entry in list) {                 int nextTag=((AutoRepairTicket) entry.Value).Tag;                 string nextTime=((AutoRepairTicket)                     entry.Value).Time.ToShortTimeString();                 Console.WriteLine("Customer: {0} Ticket: {1} Time: {2}",                     entry.Key, nextTag, nextTime);             }         }     }     public class AutoRepairTicket{         public AutoRepairTicket(string customerName,                 DateTime ticketTime) {             propName=customerName;             propTime=ticketTime;             propTag=++count;         }         private string propName;         public string Name {             get {                 return propName;             }         }         private DateTime propTime;         public DateTime Time {             get {                 return propTime;             }         }         private int propTag;         public int Tag {             get {                 return propTag;             }         }         private static int count=1000;     } } 

Stack Collection

Stack collections abstract LIFO data structures in which the initial capacity is 32 elements.

Table 5-11 lists the member of the Stack collection.

Table 5-11: Stack Members

Member Name

Syntax

Clear

This method removes all the elements of the collection.

 virtual void Clear() 

Contains

This method returns true if the specified value is found in the collection. If the value is not found, false is returned.

 virtual bool Contains(object value) 

Peek

The Peek method previews the last element on the stack. The element is returned without removal from the stack.

 virtual object Peek() 

Pop

This method returns and removes the top element of the stack.

 virtual object Pop() 

Push

This method pushes another element on the stack.

 virtual void Push(object obj) 

Synchronized

This method returns a thread-safe wrapper for the Stack collection.

 static Stack Synchronized(     Stack sourceStack) 

ToArray

This method returns the Stack collection as a regular array.

 virtual object[] ToArray() 

The following code adds numbers to a Stack collection. The values of the collection are then enumerated and displayed.

 using System; using System.Collections; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             Stack numbers=new Stack(                 new int [] {1,2,3,4,5,6});             int total=numbers.Count;     for(int count=0;count<total;++count) {                 Console.WriteLine(numbers.Pop());             }         }     } } 

Specialized Collections

In addition to the common collections that most developers use, the .NET FCL offers specialized collections. These collections are found in the System.Collections.Specialized namespace. Although these collections are used infrequently, they are valuable in certain circumstances.

Table 5-12 lists the specialized collections.

Table 5-12: Specialized Collections

Member Name

Description

BitVector32

This is an array of 32 bits. It is similar to a BitArray, but limited to 32 bits. Because of this array's refined use, BitVector32 structures are more efficient then a BitArray collection.

HybridDictionary

This collection is a combination of a ListDictionary and Hashtable. It operates as a ListDictionary when containing a small number of elements. For optimum performance, the collection switches to a Hashtable as the elements increased. ListDictionary of 10 elements or fewer is recommended.

NameValueCollection

This is a collection of keys and values, in which both the key and value are strings. The collection is accessible via an index or key. A key can refer to multiple values.

OrderedDictionary

Not documented at the time of this book.

StringCollection

This is a collection of strings.

StringDictionary

This is a combination of the Hashtable and StringCollection collections, in which both the key and value are strings.




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net