Escolar Documentos
Profissional Documentos
Cultura Documentos
Note that the GetEnumerator() method returns an enumerator object each time it is called.
Further, the loop contains the Response.Write statement in its re-initializer portion, which is
perfectly valid. The condition being evaluated is whether the MoveNext() method returns a
value of true. The MoveNext() method returns true as long as there are items in the collection.
The Current property returns the current object and is automatically typecast to string by making
a call to the ToString() method implicitly.
The foreach method can also be used in this case, as it calls the enumerator implicitly. The
above code can be re-written using a foreach loop as follows:
Listing 2
String names[]=new String[2] {”Joydip”,”Jini”};
foreach(string str in names)
Response.Write(str);
ArrayList
The ArrayList class is a dynamic array of heterogeneous objects. Note that in an array we can
store only objects of the same type. In an ArrayList, however, we can have different type of
objects; these in turn would be stored as object type only. We can have an ArrayList object that
stores integer, float, string, etc., but all these objects would only be stored as object type. An
ArrayList uses its indexes to refer to a particular object stored in its collection. The Count
property gives the total number of items stored in the ArrayList object. The Capacity property
gets or sets the number of items that the ArrayList object can contain. Objects are added using
the Add() method of the ArrayList and removed using its Remove() method. An example of
usage of an ArrayList is given below.
Listing 3
using System;
using System.Collections;
class Test
{
static void Main()
{
int i = 100;
double d = 20.5;
ArrayList arrayList = new ArrayList();
arrayList.Add("Joydip");
arrayList.Add(i);
arrayList.Add(d);
for (int index = 0; index <arrayList.Count; index++)
Console.WriteLine(arrayList[index]);
}
}
It is to be noted here that the initial capacity of an ArrayList is 16, which is increased once the
17th item is stored onto it. This repeated memory allocation and copying of the items can be
quite expensive in some situations. For performance reasons we can set the initial capacity of
the object of an ArrayList by using the Capacity property or an overloaded constructor of the
ArrayList class. This is shown in the example below.
Listing 4
using System;
using System.Collections;
class Test
{
static void Main()
{
int i = 100;
double d = 20.5;
ArrayList arrayList = new ArrayList();
arrayList.Capacity = 2;
arrayList.Add("Joydip");
arrayList.Add(i);
arrayList.Add(d);
for (int index = 0; index <arrayList.Count; index++)
Console.WriteLine(arrayList[index]);
}
}
StringCollection
The StringCollection class implements the IList interface and is like an ArrayList of strings. The
following code example shows how we can work with a StringCollection class.
Listing 5
using System;
using System.Collections;
using System.Collections.Specialized;
class Test
{
static void Main()
{
StringCollection stringList = newStringCollection();
stringList.Add("Manashi");
stringList.Add("Joydip");
stringList.Add("Jini");
stringList.Add("Piku");
}
}
StringDictionary
Similar to the StringCollection class we have the StringDictionary class, which is just a
Hashtable that has its keys as strings only. Remember that a Hashtable can contain any object
type in its key. The following code shows how we can work with a StringDictionary class.
Listing 6
using System;
using System.Collections;
using System.Collections.Specialized;
class Test
{
static void Main()
{
StringDictionary stringList = newStringDictionary();
stringList.Add("A", "Manashi");
stringList.Add("B","Joydip");
stringList.Add("C","Jini");
stringList.Add("D","Piku");
}
}
Stack
The Stack class is one that provides a Last-in-First-out (LIFO) collection of items of the
System.Object type. The last added item is always at the top of the Stack and is also the first one
to be removed. The following code sample shows how we can use a Stack class for LIFO
operation on its collection of items.
Listing 7
using System;
using System.Collections;
class Test
{
static void Main()
{
Stack stackObject = new Stack();
stackObject.Push("Joydip");
stackObject.Push("Steve");
stackObject.Push("Jini");
while (stackObject.Count > 0)
Console.WriteLine(stackObject.Pop());
Console.ReadLine();
}
}
The Push() method is responsible for storing items in the Stack and the method Pop() removes
them one at a time from the top of the Stack.
Queue
Unlike the Stack class, the Queue is a data structure that provides a First-in-First-out collection
of items of the System.Object type. The newly added items are stored at the end or the rear of
the Queue and items are deleted from the front of the Queue. The following code shows how the
Queue class can be used.
Listing 8
using System;
using System.Collections;
class Test
{
static void Main()
{
Queue queueObject = new Queue();
queueObject.Enqueue("Joydip");
queueObject.Enqueue("Steve");
queueObject.Enqueue("Jini");
while (queueObject.Count > 0)
Console.WriteLine(queueObject.Dequeue());
Console.ReadLine();
}
}
The Enqueue() method is responsible for storing items at the rear of the Queue and the method
Dequeue() removes them one at a time from the front of the Queue.
BitArray
The BitArray class can be used to store bits in an array. They can be set to true or false,
depending on the parameter supplied at the time of creating the BitArray object. The following
is an example of its usage.
BitArray bitArray = new BitArray(5,false);
Or
BitArray bitArray = new BitArray(5,true);
Similar to the other collections discussed above, the BitArray class also contains the Count
property to get the number of items stored in this collection of bit values. The following
methods of the BitArray class allow logical bit operation.
• And
• Or
• Not
• Xor
Hashtable
The Hashtable provides a faster way of storage and retrieval of items of the object type. The
Hashtable class provides support for key based searching. These keys are unique hash codes that
are unique to a specific type. The GetHashCode() method of the Hashtable class returns the hash
code for an object instance. The following code snippet shows how we can use a Hashtable
class.
Listing 9
using System;
using System.Collections;
class Test
{
static void Main()
{
Hashtable hashTable = new Hashtable();
hashTable.Add(1, "Joydip");
hashTable.Add(2, "Manashi");
hashTable.Add(3, "Jini");
hashTable.Add(4, "Piku");
Console.WriteLine("The keysare:--");
foreach (int k in hashTable.Keys)
{
Console.WriteLine(k);
}
To remove an item from the Hashtable class, the Remove() method is used. The statement
hashTable.Remove(3) would remove the item “Jini” from the Hashtable object created in the
above code. The code shown above can also be written as shown below to display the contents
of the Hashtable object using IDictionaryEnumerator.
Listing 10
using System;
using System.Collections;
class Test
{
static void Main()
{
Hashtable hashTable = new Hashtable();
hashTable.Add(1, "Joydip");
hashTable.Add(2, "Manashi");
hashTable.Add(3, "Jini");
hashTable.Add(4, "Piku");
Console.WriteLine("The keysare:--");
IDictionaryEnumerator en =hashTable.GetEnumerator();
string str = String.Empty;
while (en.MoveNext())
{
str = en.Value.ToString();
Console.WriteLine(str);
}
}
}
SortedList
The SortedList class allows items of the System.Object type to be placed in the collection using
key value pairs and, at the same time, supports sorting. The following code shows how we can
use a SortedList.
Listing 11
using System;
using System.Collections;
using System.Collections.Specialized;
class Test
{
static void Main()
{
SortedList sortedList = new SortedList();
sortedList.Add(1, "Manashi");
sortedList.Add(3, "Joydip");
sortedList.Add(2, "Jini");
sortedList.Add(4, "Piku");
Console.WriteLine("Displaying thenames");
}
}
class Test
{
static void Main()
{
SortedList sortedList = new SortedList();
sortedList.Add(1, "Manashi");
sortedList.Add(3, "Joydip");
sortedList.Add(2, "Jini");
sortedList.Add(4, "Piku");
Console.WriteLine("Displaying thenames");
IDictionaryEnumerator en = sortedList.GetEnumerator();
string str = String.Empty;
while (en.MoveNext())
{
str = en.Value.ToString();
Console.WriteLine(str);
}
}
}
The following code shows how we can use the "design a custom collection class" by sub-classing
the DictionaryBase class.
Listing 14
using System;
using System.Collections;