Skip to main content

Collections in .Net



Namespace: System.Collections

Types of Collections:
Class
Description
ArrayList
Implements the IList interface using an array whose size is dynamically increased as required.
BitArray
Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).
CaseInsensitiveComparer
Compares two objects for equivalence, ignoring the case of strings.
CaseInsensitiveHashCodeProvider
Obsolete. Supplies a hash code for an object, using a hashing algorithm that ignores the case of strings.
CollectionBase
Provides the abstract base class for a strongly typed collection.
Comparer
Compares two objects for equivalence, where string comparisons are case-sensitive.
DictionaryBase
Provides the abstract base class for a strongly typed collection of key/value pairs.
Hashtable
Represents a collection of key/value pairs that are organized based on the hash code of the key.
Queue
Represents a first-in, first-out collection of objects.
ReadOnlyCollectionBase
Provides the abstract base class for a strongly typed non-generic read-only collection.
SortedList
Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
Stack
Represents a simple last-in-first-out (LIFO) non-generic collection of objects.

Intefaces:
Interface
Description
ICollection
Defines size, enumerators, and synchronization methods for all nongeneric collections.
IComparer
Exposes a method that compares two objects.
IDictionary
Represents a nongeneric collection of key/value pairs.
IDictionaryEnumerator
Enumerates the elements of a nongeneric dictionary.
IEnumerable
Exposes the enumerator, which supports a simple iteration over a non-generic collection.
IEnumerator
Supports a simple iteration over a nongeneric collection.
IEqualityComparer
Defines methods to support the comparison of objects for equality.
IHashCodeProvider
Obsolete. Supplies a hash code for an object, using a custom hash function.
IList
Represents a non-generic collection of objects that can be individually accessed by index.

ArrayList
The ArrayList class is a simple, unordered container for objects of any type.
Add, AddRange(To add items from another collection or ArrayList),

Array List supports the IEnumerable interface that exposes GetEnumerator method that return IEnumerator interface. IEnumerator have a property called Current and two methods including MoveNext and Reset.

ArrayList collection = new ArrayList();
IEnumerator e = collection.GetEnumerator();
While(e.MoveNext())
{
                Console.WriteLine(e.Current);
}

OR
foreach(object o in collection)
                Console.WriteLine(o.ToString());

Consistent Interfaces in Collections:
Like IEnumerable interface, all collection classes inherit from ICollection interface that intern inherit from IEnumerable interface. ICollection interface provide you the following Proepries:

Name
Description
Count
Gets the number of elements contained in the ICollection.
IsSynchronized
Gets a value indicating whether access to the ICollection is synchronized (thread safe).
SyncRoot
Gets an object that can be used to synchronize access to the ICollection.

Important Methods:
Name
Description
CopyTo
Copies the elements of the ICollection to an Array, starting at a particular Array index.
GetEnumerator
Returns an enumerator that iterates through a collection. (Inherited from IEnumerable.)

Some simple list type collections inherit from IList interface that intern inherit from ICollection interface.

IList Interface’s most important properties are:
Name
Description
Count
Gets the number of elements contained in the ICollection. (Inherited from ICollection.)
IsFixedSize
Gets a value indicating whether the IList has a fixed size.
IsReadOnly
Gets a value indicating whether the IList is read-only.
IsSynchronized
Gets a value indicating whether access to the ICollection is synchronized (thread safe). (Inherited from ICollection.)
Item
Gets or sets the element at the specified index.
SyncRoot
Gets an object that can be used to synchronize access to the ICollection. (Inherited from ICollection.)

IList Interface’s most important Methods are:

Name
Description
Add
Adds an item to the IList.
Clear
Removes all items from the IList.
Contains
Determines whether the IList contains a specific value.
CopyTo
Copies the elements of the ICollection to an Array, starting at a particular Array index. (Inherited from ICollection.)
GetEnumerator
Returns an enumerator that iterates through a collection. (Inherited from IEnumerable.)
IndexOf
Determines the index of a specific item in the IList.
Insert
Inserts an item to the IList at the specified index.
Remove
Removes the first occurrence of a specific object from the IList.
RemoveAt
Removes the IList item at the specified index.

Inheritance Sequence:

IEnumerator IEnumerable ICollection IList

Sorting Items:
                        To sort item in an ArrayList, simply call ArrayList.Sort method. This method works using Comparer class. 
Comparer is the default implementation of the ICompare interface. One can specify any other Compare class that implements the IComparer interface. Like another built-in implementation that perform Case Insensitive comparison i.e. CaseInsensitiveComparer.

Comments

Popular posts from this blog

Culture Information and Localization in .NET

Namespace: System.Globalization CultureInfo Class:                 It provides information like the Format of numbers and dates, Culture’s Calendar, Culture’s language and sublanguage (if applicable), Country and region of the culture. The Basic use of CultureInfo class is shown here: • How string Comparisons are performed • How Number Comparison & Formats are performed • Date Comparison and Formats. • How resources are retrieved and used. Cultures are grouped into three categories: Invariant Culture : It’s Culture Insensitive. It can be used to build some trial application. It can be also used to build an application with hard-coded expiry date that ignores cultures. But using it for every comparison will be incorrect and inappropriate. Neutral Culture : English(en), Frensh(fr), and Spanish(sp). A neutral culture is related to language but it’s not related to specific regi...

Concept of App Domain in .Net

Creating Application Domains: Application domain is just like process, provides separate memory space, and isolates from other code. But it’s quite light weight. It also provides the following advantages: 1-       Reliability : If a domain crashes, it can be unloaded. Hence doesn’t affect the other assemblies. 2-       Efficiency : Loading all assemblies in one domain can be cumbersome and can make the process heavy but Appdomains are efficient in this manner. Important properties of AppDomain: ApplicationIdentity , ApplicationTrust , BaseDirectory , CurrentDomain , DomainManager , DomainDirectory , Evidence , FriendlyName , ID , RelativeSearchPath , SetupInformation , ShadowCopyFiles . Important methods of AppDomain: ApplyPolicy , CreateCOMInstanceFrom , CreateDomain , CreateInstance (Assembly). To create an AppDomain: AppDomain adomain = AppDomain.CreateDomain(“D”); To execute an assembly:...

Asynchronous Execution in ASP.NET

Asynchronous Execution: Two ways either implement IHTTPAsyncHandler interface or in ASP.NET 2.0 set <%@ Page Async=”true” %>. The second option implements IHTTPAsyncHandler interface automatically for the page when parsed or compiled. AddOnPreRenderCompleteAsync ( new BeginEventHandler(BeginTask), new EndEventHandler(EndTask)); AddOnPreRenderCompleteAsync() shoud be called in Page_load. The BeginEventHandler and EndEventHandler are delegates defined as follows: IAsyncResult BeginEventHandler( object sender, EventArgs e, AsyncCallback cb, object state) void EndEventHandler( IAsyncResult ar) AsyncProcess starts and completes between PreRender and PreRenderComplete. Other way to perform Async Task is using AsyncPageTask structure. It also allows multiple tasks to execute simultaneously. void Page_Load (object sender, EventArgs e) { PageAsyncTask task = new PageAsyncTask( new BeginEventHandler(BeginTask), new EndEventH...