Sunday, May 20, 2018

C# : Collection

C# also includes specialized classes that hold many values or objects in a specific series, that are called 'collection'.
There are two types of collections available in C#: non-generic collections and generic collections
Every collection class implements the IEnumerable interface so values from the collection can be accessed using a foreach loop.
The System.Collections namespace includes following non-generic collections.
Non-generic CollectionsUsage
ArrayListArrayList stores objects of any type like an array. However, there is no need to specify the size of the ArrayList like with an array as it grows automatically.
SortedListSortedList stores key and value pairs. It automatically arranges elements in ascending order of key by default. C# includes both, generic and non-generic SortedList collection.
StackStack stores the values in LIFO style (Last In First Out). It provides a Push() method to add a value and Pop() & Peek() methods to retrieve values. C# includes both, generic and non-generic Stack.
QueueQueue stores the values in FIFO style (First In First Out). It keeps the order in which the values were added. It provides an Enqueue() method to add values and a Dequeue() method to retrieve values from the collection. C# includes generic and non-generic Queue.
HashtableHashtable stores key and value pairs. It retrieves the values by comparing the hash value of the keys.
BitArrayBitArray 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).

No comments:

Post a Comment