Вы находитесь на странице: 1из 14

COLLECTIONS

COLLECTION
The .NET framework provides specialized classes for data storage and retrieval. In one of the previous chapters, we have described arrays. Collections are enhancement to the arrays. There are two distinct collection types in C#.
The standard collections, which are found under the System. Collections namespace and the generic collections, under System.Collections.Generic.

The generic collections are more flexible and are the preferred way to work with data. The generic collections or generics were introduced in .NET framework 2.0. Generics enhance code reuse, type safety, and performance.

Collection classes are used for data storage and manipulate (sort, insert, remove etc) the data. There are mainly two types of collections
Generic collection(List)(System.Collections.Generic) Non-Generic collection(Arraylist and Hashtable) (System.Collections)

LIST
A List is a strongly typed list of objects that can be accessed by index. It can be found under System.Collections.Generic namespace First, the List type provides you with an efficient and dynamically-allocated array. It does not provide fast lookup in the general case, which you will want to use Dictionary for. It is excellent when used in loops.

Add the item in List<T> Example: //define the List here List<string> countryList = new List<string>(); //use the add method to add the element in List countryList.Add("Rusea"); countryList.Add("GreenLand"); countryList.Add("India"); countryList.Add("Pakistan"); countryList.Add("US"); //print the data on web page Response.Write("<b><u>Country List:</u></b><br/>"); foreach (string country in countryList) Response.Write(country +"<br/>"); Output Country List: India SriLanka SouthAfrica Australia England

Remove the item from List<T>


List have following remove properties: Remove(), RemoveAt(), RemoveAll(), RemoveRange(). Example:

countryList.Remove("Pakistan"); Insert item into the List<T>


Insert method is used for Insert item into the List at any index of the list. Example countryList.Insert(2,"Pakistan");

Sort the item from the List<T>


Example: countryList.Sort ();

Program that uses List type [C#] using System; using System.Collections.Generic; class Program {
static void Main() { // Use the List type. List<string> list = new List<string>(); list.Add("cat"); list.Add("dog"); foreach (string element in list) { Console.WriteLine(element); }

}
} Output cat dog

ARRAYLIST
ArrayList is a collection from a standard System.Collections namespace. It is a dynamic array. It provides random access to its elements. An ArrayList automatically expands as data is added. Unlike arrays, an ArrayList can hold data of multiple data types. Elements in the ArrayList are accessed via an integer index. Indexes are zero based. Indexing of elements and insertion and deletion at the end of the ArrayList takes constant time. Inserting or deleting an element in the middle of the dynamic array is more costly. It takes linear time.

ArrayList is a collection found in System.Collections and it can store objects of any derived type. You don't need to worry about the type of the elements, at least until you need to use them. 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.

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]); } }

HASHTABLE
HashTable: Represents a collection of key/value pairs that are organized based on the hash code of the key. Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be null, but a value can be. When an element is added to the Hashtable, the element is placed into a bucket based on the hash code of the key. Subsequent lookups of the key use the hash code of the key to search in only one particular bucket, thus substantially reducing the number of key comparisons required to find an element.

HashTable Hashtable in C# represents a collection of key/value pairs which maps keys to value. Any non-null object can be used as a key but a value can. We can retrieve items from hashTable to provide the key. Both keys and values are Objects. The main properties of HashTable are Key and Values and methods add (), remove (), contains () etc 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.

How use HashTable in .net Example: //make object of HashTable class like countryTable Hashtable countryTable = new Hashtable(); //Add the country in the hashtable.Add(Object key,Object value) countryTable.Add(1, "India"); countryTable.Add(2, "Srilanka"); countryTable.Add(3, "England"); //Find the key and value by using DictionaryEntry class foreach (DictionaryEntry country in countryTable) Response.Write(country.Key + " : " + country.Value +"<br/>"); Output: Country List: 3 : England 2 : Srilanka 1 : India

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); } Console.WriteLine("Please enter the keyto search"); int p = int.Parse(Console.ReadLine()); Console.WriteLine(hashTable[3].ToString()); } }

Вам также может понравиться