Ads block

Banner 728x90px

c#step30



C# Collection:

We have learned about an array in the previous section. 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. We will learn about non-generic collections in this section.
The System.Collections namespace includes the interfaces and classes for the non-generic collections. The following diagram illustrates the hierarchy of the interfaces and classes for the non-generic collections.
C# Collections
As you can see in the above diagram, IEnumerator, IEnumerable, and ICollection are the top level interfaces for all the collections in C#.
IEnumerator: The IEnumerator interface supports a simple iteration over a non-generic collection. It includes methods and property which can be implemented to support easy iteration using foreach loop.
IEnumerable: The IEnumerable interface includes GetEnumerator() method which returns an object of IEnumerator.
So, all the built-in collection classes and custom collection classes must implement IEnumerator and IEnumerable interfaces for easy iteration using foreach loop.
ICollection: The ICollection interface is the base interface for all the collections that defines sizes, enumerators, and synchronization methods for all non-generic collections. The Queue and Stack collection implement ICollection inferface.
IList: The IList interface includes properties and methods to add, insert, remove elements in the collection and also individual element can be accessed by index. The ArrayList and BitArray collections implement IList interface.
IDictionary: The IDictionary interface represents a non-generic collection of key/value pairs. The Hashtable and SortedList implement IDictionary interface and so they store key/value pairs.
As you can see from the diagram, ArrayList, BitArray, Hashtable, SortedList, Queue, and Stack collections implement different interfaces and so, they are used for the different purposes.
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).
Let's see each type of collection in the next chapters.

C# - ArrayList

ArrayList is a non-generic type of collection in C#. It can contain elements of any data types. It is similar to an array, except that it grows automatically as you add items in it. Unlike an array, you don't need to specify the size of ArrayList.
The following diagram illustrates the ArrayList class hierarchy.
C# ArrayList
As you can see from the above diagram, the ArrayList class implements IEnumerableICollection, and IList interfaces. So, you can create an object of ArrayList class and assign it to the variable of any base interface type. However, if you assign it to IEnumerable or ICollection type variable then you won't be able to add elements and access ArrayList by index.
Example: Create ArrayList
ArrayList myArryList1 = new ArrayList();// Recommended
// or - with some limitations
IList myArryList2 = new ArrayList();
// or - with some limitations
ICollection myArryList3 = new ArrayList();
// or - with some limitations
IEnumerable myArryList4 = new ArrayList();
It is recommended to use ArrayList type of variable for ArrayList object because ArrayList class contains some additional methods which are not the members of IListinterface such as AddRange()BinarySearch()Repeat()Reverse(), etc.

Important Properties and Methods of ArrayList

PropertiesDescription
CapacityGets or sets the number of elements that the ArrayList can contain.
CountGets the number of elements actually contained in the ArrayList.
IsFixedSizeGets a value indicating whether the ArrayList has a fixed size.
IsReadOnlyGets a value indicating whether the ArrayList is read-only.
ItemGets or sets the element at the specified index.
MethodsDescription
Add()/AddRange()Add() method adds single elements at the end of ArrayList. 
AddRange() method adds all the elements from the specified collection into ArrayList.
Insert()/InsertRange()Insert() method insert a single elements at the specified index in ArrayList. 
InsertRange() method insert all the elements of the specified collection starting from specified index in ArrayList.
Remove()/RemoveRange()Remove() method removes the specified element from the ArrayList. 
RemoveRange() method removes a range of elements from the ArrayList.
RemoveAt()Removes the element at the specified index from the ArrayList.
Sort()Sorts entire elements of the ArrayList.
Reverse()Reverses the order of the elements in the entire ArrayList.
ContainsChecks whether specified element exists in the ArrayList or not. Returns true if exists otherwise false.
ClearRemoves all the elements in ArrayList.
CopyToCopies all the elements or range of elements to compitible Array.
GetRangeReturns specified number of elements from specified index from ArrayList.
IndexOfSearch specified element and returns zero based index if found. Returns -1 if element not found.
ToArrayReturns compitible array from an ArrayList.

Adding Elements into ArrayList

The AddRange() method can take any type of collection that implements the ICollection interface e.g. List, ArrayList, SortedList, Queue, Stack, HashSet, Hashtable, etc.
Use the Add()method to add a single element or the AddRange() method to add multiple elements from the other collections into an ArrayList. Here, the element means the literal value of a primitive or non-primitive type. Please note that the AddRange() method only available in the ArrayList class but not in IList. So, it can only be use with the variable of type ArrayList.
Signature: int Add(Object value) 
void AddRange(ICollection c)
Example: Adding Elements into ArrayList
ArrayList arryList1 = new ArrayList();
arryList1.Add(1);
arryList1.Add("Two");
arryList1.Add(3);
arryList1.Add(4.5);

IList arryList2 = new ArrayList()
{
    100, 200
};

//Adding entire collection using ArrayList.AdRange() method.
////Note: IList does not contain AddRange() method.
arryList1.AddRange(arryList2);
You can also add items when you initialize it using object initializer syntax.
IList arrayList = new ArrayList() { 100, "Two", 12.5, 200 };
ArrayList can contain multiple null and duplicate values.

Accessing Elements

ArrayList elements can be accessed using indexer, in the same way as an array. However, you need to cast it to the appropriate type or use the implicit type varkeyword while accessing it.
Example: Accessing Elements
ArrayList myArryList = new ArrayList();
myArryList.Add(1);
myArryList.Add("Two");
myArryList.Add(3);
myArryList.Add(4.5f);

//Access individual item using indexer
int firstElement = (int) myArryList[0]; //returns 1
string secondElement = (string) myArryList[1]; //returns "Two"
int thirdElement = (int) myArryList[2]; //returns 3
float fourthElement = (float) myArryList[3]; //returns 4.5

//use var keyword
var firstElement = myArryList[0]; //returns 1
Use a foreach or a for loop to iterate an ArrayList.
Example: Iterate ArrayList
IList myArryList = new ArrayList()
                        {
                            1,
                            "Two",
                            3,
                            4.5F
                        };

foreach (var val in myArryList)
    Console.WriteLine(val); 
            
//Or
for(int i = 0 ; i< myArryList.Count; i++)
    Console.WriteLine(myArryList[i]);
Output:

Two 

4.5

Inserting Elements into ArrayList

Use the IList.Insert() method to insert a single item at the specified index.
Signature: void Insert(int index, Object value)
Example: Insert()
IList myArryList = new ArrayList();
myArryList.Add(1);
myArryList.Add("Two");
myArryList.Add(3);
myArryList.Add(4.5);

myArryList.Insert(1, "Second Item");
myArryList.Insert(2, 100);

foreach (var val in myArryList)
        Console.WriteLine(val); 
Use the ArrayList.InsertRange() method to insert all the values from another collection into ArrayList at the specfied index. Please note that the InsertRange()method only available in the ArrayList class but not in IList. So, it can only be use with the variable of type ArrayList.
Signature: Void InsertRange(int index, ICollection c)
Example: InsertRange()
IList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);

ArrayList arryList2 = new ArrayList();
arryList2.Add(10);
arryList2.Add(20);
arryList2.Add(30);

arryList2.InsertRange(2, arryList1);

foreach(var item in arryList2)
    Console.WriteLine(item);
Output:
10 
20 
100 
200 
30

Remove Elements from ArrayList

Use the IList.Remove() method to remove a specified element from an ArrayList.
Signature: void Remove(Object obj)
Example: Remove()
IList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);

arryList1.Remove(100); //Removes 1 from ArrayList

foreach (var item in arryList1)
    Console.WriteLine(item);
Output:
200 
300

Use the IList.RemoveAt() method to remove an element from the specified index location.
Signature: void RemoveAt(int index)
Example: RemoveAt()
IList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);

arryList1.RemoveAt(1); //Removes the first element from an ArrayList

foreach (var item in arryList1)
    Console.WriteLine(item);
Output:
100 
300

Use the ArrayList.RemoveRange() method to remove multiple elements from the specified index till the specified number of elements in the ArrayList. Please note that the RemoveRange() method only available in the ArrayList class but not in IList. So, it can only be use with the variable of type ArrayList.
Signature: void RemoveRange(int index, int count)
Example: RemoveRange()
ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);

arryList1.RemoveRange(0,2);//Removes two elements starting from 1st item (0 index)

foreach(var item in arryList1)
    Console.WriteLine(item);
Output:
300

ArrayList Sorting

The ArrayList class includes Sort() and Reverse() method. The Sort() method arranges elements in ascending order. However, all the elements should have same data type so that it can compare with default comparer otherwise it will throw runtime exception.
The Reverse() method arranges elements in reverse order. Last element at zero index and so on.
Example: Sort(), Reverse()
ArrayList arryList1 = new ArrayList();
arryList1.Add(300);
arryList1.Add(200);
arryList1.Add(100);
arryList1.Add(500);
arryList1.Add(400);

Console.WriteLine("Original Order:");

foreach(var item in arryList1)
    Console.WriteLine(item);

arryList1.Reverse();
Console.WriteLine("Reverse Order:");

foreach(var item in arryList1)
    Console.WriteLine(item);

arryList1.Sort();
Console.WriteLine("Ascending Order:");

foreach(var item in arryList1)
    Console.WriteLine(item);

Check for an Existing Elements in ArrayList

The IList.Contains() method checks whether specified element exists in the ArrayList or not. Returns true if exists otherwise false.
Example: Contains()
IList myArryList = new ArrayList();
myArryList.Add(100);
myArryList.Add("Hello World");
myArryList.Add(300);

Console.WriteLine(myArryList.Contains(100)); // true
 Further Reading
  • Difference between Array & ArrayList
  • ArrayList's Methods & Properties 
 Points to Remember :
  1. ArrayList can store items(elements) of any datatype.
  2. ArrayList resizes automatically as you add the elements.
  3. ArrayList values must be cast to appropriate data types before using it.
  4. ArrayList can contain multiple null and dulplicate items.
  5. ArrayList can be accessed using foreach or for loop or indexer.
  6. Use Add(), AddRange(), Remove(), RemoveRange(), Insert(), InsertRange(), Sort(), Reverse() methods.

C# - SortedList

The SortedList collection stores key-value pairs in the ascending order of key by default. SortedList class implements IDictionary & ICollection interfaces, so elements can be accessed both by key and index.
C# includes two types of SortedList, generic SortedList and non-generic SortedList. Here, we will learn about non-generic SortedList.
The following diagram illustrates the non-generic SortedList hierarchy.
C# SortedList

Important Properties and Methods of SortedList

PropertyDescription
CapacityGets or sets the number of elements that the SortedList instance can store.
CountGets the number of elements actually contained in the SortedList.
IsFixedSizeGets a value indicating whether the SortedList has a fixed size.
IsReadOnlyGets a value indicating whether the SortedList is read-only.
ItemGets or sets the element at the specified key in the SortedList.
KeysGet list of keys of SortedList.
ValuesGet list of values in SortedList.
MethodDescription
Add(object key, object value)Add key-value pairs into SortedList.
Remove(object key)Removes element with the specified key.
RemoveAt(int index)Removes element at the specified index.
Contains(object key)Checks whether specified key exists in SortedList.
Clear()Removes all the elements from SortedList.
GetByIndex(int index)Returns the value by index stored in internal array
GetKey(int index)Returns the key stored at specified index in internal array
IndexOfKey(object key)Returns an index of specified key stored in internal array
IndexOfValue(object value)Returns an index of specified value stored in internal array

Add elements in SortedList

Use the Add() method to add key-value pairs into a SortedList.
Add() method signature: void Add(object key, object value)
Key cannot be null but value can be null. Also, datatype of all keys must be same, so that it can compare otherwise it will throw runtime exception.
Example: Add key-value pairs in SortedList
SortedList sortedList1 = new SortedList();
sortedList1.Add(3, "Three");
sortedList1.Add(4, "Four");
sortedList1.Add(1, "One");
sortedList1.Add(5, "Five");
sortedList1.Add(2, "Two");

SortedList sortedList2 = new SortedList();
sortedList2.Add("one", 1);
sortedList2.Add("two", 2);
sortedList2.Add("three", 3);
sortedList2.Add("four", 4);
    
SortedList sortedList3 = new SortedList();
sortedList3.Add(1.5, 100);
sortedList3.Add(3.5, 200);
sortedList3.Add(2.4, 300);
sortedList3.Add(2.3, null);
sortedList3.Add(1.1, null);
 Note:
Internally, SortedList maintains two object[] array, one for keys and another for values. So when you add key-value pair, it runs a binary search using the key to find an appropriate index to store a key and value in respective arrays. It re-arranges the elements when you remove the elements from it.
SortedList collection sorts the elements everytime you add the elements. So if you debug the above example, you will find keys in ascending order even if they are added randomly, as below:
SortedList in debug view
Please notice that sortedList2 sorts the key in alphabetical order for string key in the above image.
Use Object Initializer Syntax to initialize the SortedList, as shown below.
Example: Add key-value pairs in SortedList
SortedList sortedList = new SortedList()
{
    {3, "Three"},
    {4, "Four"},
    {1, "One"},
    {5, "Five"},
    {2, "Two"}
};
SortedList key can be of any data type, but you cannot add keys of different data types in the same SortedList. The key type of the first key-value pair remains the same for all other key-value pairs. The following example will throw run time exception because we are trying to add the second item with a string key:
Example: Key of different datatypes throws exception:
SortedList sortedList = new SortedList();

sortedList.Add(3, "Three");
sortedList.Add("Four", "Four"); // Throw exception: InvalidOperationException
sortedList.Add(1, "One");
sortedList.Add(8, "Five");
sortedList.Add(2, "Two");

Access SortedList

SortedList can be accessed by index or key. Unlike other collection, SortedList requires key instead of index to access a value for that key.
Example: Access SortedList
SortedList sortedList = new SortedList()
                            {
                                {"one", 1},
                                {"two", 2},
                                {"three", 3},
                                {"four", "Four"}
                            }

int i = (int) sortedList["one"];
int j = (int) sortedList["two"];
string str = (string) sortedList["four"];

Console.WriteLine(i);
Console.WriteLine(j);
Console.WriteLine(str);
Output:


Four
 Note:
Non-generic SortedList collection can contain key and value of any data type. So values must be cast to the appropriate data type otherwise it will give compile-time error.
Use for loop to access SortedList as shown below.
Example: Accessing Values using For Loop
SortedList sortedList = new SortedList()
                            {
                                {3, "Three"},
                                {4, "Four"},
                                {1, "One"},
                                {5, "Five"},
                                {2, "Two"}
                            };
for (int i = 0; i < sortedList.Count; i++)
{
    Console.WriteLine("key: {0}, value: {1}", 
        sortedList.GetKey(i), sortedList.GetByIndex(i));
}
Output:
key: 1, value: One 
key: 2, value: Two 
key: 3, value: Three 
key: 4, value: Four 
key: 5, value: Five
The foreach statement can also be used to access the SortedList collection. SortedList includes key-value pairs. So, the type of element would be DictionaryEntry rather than the type of a key or a value.
Example: Access values using foreach
SortedList sortedList = new SortedList()
                            {
                                {3, "Three"},
                                {4, "Four"},
                                {1, "One"},
                                {5, "Five"},
                                {2, "Two"}
                            };
foreach(DictionaryEntry kvp in sortedList )
    Console.WriteLine("key: {0}, value: {1}", kvp.Key , kvp.Value );

Remove elements from SortedList

Use the Remove() or RemoveAt() method to remove elements from a SortedList.
Remove() signature: void Remove(object key)
RemoveAt() signature: void RemoveAt(int index)
Example: Remove elements in SortedList
SortedList sortedList = new SortedList();
sortedList.Add("one", 1);
sortedList.Add("two", 2);
sortedList.Add("three", 3);
sortedList.Add("four", 4);
    
sortedList.Remove("one");//removes element whose key is 'one'
sortedList.RemoveAt(0);//removes element at zero index i.e first element: four

foreach(DictionaryEntry kvp in sortedList )
    Console.WriteLine("key: {0}, value: {1}", kvp.Key , kvp.Value );
Output:
key: three, value: 3 
key: two, value: 2

Check for an existing key in SortedList

The Contains() & ContainsKey() methods determine whether the specified key exists in the SortedList collection or not.
Contains() signature: bool Contains(object key)
ContainsKey() signature: bool ContainsKey(object key)
The ContainsValue() method determines whether the specified value exists in the SortedList or not.
ContainValue() signature: bool ContainValue(object value)
Example: Contains
SortedList sortedList = new SortedList();
                            {
                                {3, "Three"},
                                {4, "Four"},
                                {1, "One"},
                                {8, "Eight"},
                                {2, "Two"}
                            };
sortedList.Contains(2); // returns true
sortedList.Contains(4); // returns true
sortedList.Contains(6); // returns false

sortedList.ContainsKey(2); // returns true
sortedList.ContainsKey(6); // returns false

sortedList.ContainsValue("One"); // returns true
sortedList.ContainsValue("Ten"); // returns false
Visit MSDN for more information on the properties and methods of SortedList.
 Points to Remember :
  1. C# has generic and non-generic SortedList.
  2. SortedList stores the key-value pairs in ascending order of the key. Key must be unique and cannot be null whereas value can be null or duplicate.
  3. Non-generic SortedList stores keys and values of any data types. So values needs to be cast to appropriate data type.
  4. Key-value pair can be cast to DictionaryEntry.
  5. Access individual value using indexer. SortedList indexer accepts key to return value associated with it.

C# - Hashtable

C# includes Hashtable collection in System.Collections namespace, which is similar to generic Dictionary collection. The Hashtable collection stores key-value pairs. It optimizes lookups by computing the hash code of each key and stores it in a different bucket internally and then matches the hash code of the specified key at the time of accessing values.
The following diagram illustrates the Hashtable class hierarchy.
C# Hashtable

Important Propertis and Methods of Hashtable

PropertyDescription
CountGets the total count of key/value pairs in the Hashtable.
IsReadOnlyGets boolean value indicating whether the Hashtable is read-only.
ItemGets or sets the value associated with the specified key.
KeysGets an ICollection of keys in the Hashtable.
ValuesGets an ICollection of values in the Hashtable.
MethodsUsage
AddAdds an item with a key and value into the hashtable.
RemoveRemoves the item with the specified key from the hashtable.
ClearRemoves all the items from the hashtable.
ContainsChecks whether the hashtable contains a specific key.
ContainsKeyChecks whether the hashtable contains a specific key.
ContainsValueChecks whether the hashtable contains a specific value.
GetHashReturns the hash code for the specified key.

Add key-value into Hashtable

The Add() method adds an item with a key and value into the Hashtable. Key and value can be of any data type. Key cannot be null whereas value can be null.
Add() Signature: void Add(object key, object value);
Example: Add()
Hashtable ht = new Hashtable();

ht.Add(1, "One");
ht.Add(2, "Two");
ht.Add(3, "Three");
ht.Add(4, "Four");
ht.Add(5, null);
ht.Add("Fv", "Five");
ht.Add(8.5F, 8.5);
You can also assign key and value at the time of initialization using object initializer syntax:
Example: Add()
Hashtable ht = new Hashtable()
                {
                    { 1, "One" },
                    { 2, "Two" },
                    { 3, "Three" },
                    { 4, "Four" },
                    { 5, null },
                    { "Fv", "Five" },
                    { 8.5F, 8.5 }
                };
Hashtable can include all the elements of Dictionary as shown below.
Example: Add()
Dictionary<int, string> dict = new Dictionary<int, string>();

dict.Add(1, "one");
dict.Add(2, "two");
dict.Add(3, "three");

Hashtable ht = new Hashtable(dict);
 
Note : Add() will throw an exception if you try to add a key that already exists in the Hashtable. So always check the key using the Contains() or ContainsKey() method before adding a key-value pair into the Hashtable.

Access Hashtable

You can retrive the value of an existing key from the Hashtable using indexer. Please note that the hashtable indexer requires a key.
Example: Access Hashtable
Hashtable ht = new Hashtable();

ht.Add(1, "One");
ht.Add(2, "Two");
ht.Add(3, "Three");
ht.Add(4, "Four");
ht.Add("Fv", "Five");
ht.Add(8.5F, 8.5F);
    
string strValue1 = (string)ht[2];
string strValue2 = (string)ht["Fv"];
float fValue = (float) ht[8.5F];

Console.WriteLine(strValue1);
Console.WriteLine(strValue2);
Console.WriteLine(fValue);
Output:
Two 
Five 
8.5
 Note:
Hashtable is a non-generic collection so it can contains a key and a value of any data type. So values must be cast to an appropriate data type otherwise it will give compile-time error.
Hashtable elements are key-value pairs stored in DictionaryEntry. So you cast each element in Hashtable to DictionaryEntry. Use the foreach statement to iterate the Hashtable, as shown below:
Example: Iterate Hashtable
Hashtable ht = new Hashtable()
                {
                    { 1, "One" },
                    { 2, "Two" },
                    { 3, "Three" },
                    { 4, "Four" },
                    { 5, null },
                    { "Fv", "Five" },
                    { 8.5F, 8.5 }
                };

foreach (DictionaryEntry item in ht)
                Console.WriteLine("key:{0}, value:{1}",item.Key, item.Value);
Output:
Key: Fv, Value: Five 
Key: 8.5, Value: 8.5 
Key: 5, Value: 
Key: 4, Value: Four 
Key: 3, Value: Three 
Key: 2, Value: Two 
Key: 1, Value: One
Hashtable has a Keys and a Values property that contain all the keys and values respectively. You can use these properties to get the keys and values.
Example: Access Hashtable using Keys & Values
Hashtable ht = new Hashtable();
                {
                    { 1, "One" },
                    { 2, "Two" },
                    { 3, "Three" },
                    { 4, "Four" },
                    { 5, null },
                    { "Fv", "Five" },
                    { 8.5F, 8.5 }
                };

foreach (var key in ht.Keys )
                Console.WriteLine("Key:{0}, Value:{1}",key , ht[key]);

Console.WriteLine("***All Values***");
        
foreach (var value in ht.Values)
                Console.WriteLine("Value:{0}", value);

Remove elements in Hashtable

The Remove() method removes the item with the specified key from the hashtable.
Remove() Method Signature: void Remove(object key)
Example: Remove()
Hashtable ht = new Hashtable()
                {
                    { 1, "One" },
                    { 2, "Two" },
                    { 3, "Three" },
                    { 4, "Four" },
                    { 5, null },
                    { "Fv", "Five" },
                    { 8.5F, 8.5 }
                };

ht.Remove("Fv"); // removes {"Fv", "Five"}

Check for Existing Elements

Contains() and ContainsKey() check whether the specified key exists in the Hashtable collection. ContainsValue() checks whether the specified value exists in the Hashtable.
Contains(), ContainsKey() and ContainsValue() Signatures:
bool Contains(object key) 
bool ContainsKey(object key) 
bool ContainsValue(object value) 
Example: Contains
Hashtable ht = new Hashtable()
                {
                    { 1, "One" },
                    { 2, "Two" },
                    { 3, "Three" },
                    { 4, "Four" }
                };

ht.Contains(2);// returns true
ht.ContainsKey(2);// returns true
ht.Contains(5); //returns false
ht.ContainsValue("One"); // returns true

Clear()

Clear() method removes all the key-value pairs in the Hashtable.
Clear() Method Signature: void Clear()
Example: Clear()
Hashtable ht = new Hashtable()
                {
                    { 1, "One" },
                    { 2, "Two" },
                    { 3, "Three" },
                    { 4, "Four" },
                    { 5, null },
                    { "Fv", "Five" },
                    { 8.5F, 8.5 }
                };

ht.Clear(); // removes all elements
Console.WriteLine("Total Elements: {0}", ht.Count);
 Points to Remember :
  1. Hashtable stores key-value pairs of any datatype where the Key must be unique.
  2. The Hashtable key cannot be null whereas the value can be null.
  3. Hashtable retrieves an item by comparing the hashcode of keys. So it is slower in performance than Dictionary collection.
  4. Hashtable uses the default hashcode provider which is object.GetHashCode(). You can also use a custom hashcode provider.
  5. Use DictionaryEntry with foreach statement to iterate Hashtable.

C# - Stack

C# includes a special type of collection which stores elements in LIFO style (Last In First Out). C# includes a generic and non-generic Stack. Here, you are going to learn about the non-generic stack.
Stack allows null value and also duplicate values. It provides a Push() method to add a value and Pop() or Peek() methods to retrieve values.
C# stack
C# Stack
The following diagram illustrates the Stack class hierarchy.
C# stack
C# Stack

Important Properties and Methods of Stack:

PropertyUsage
CountReturns the total count of elements in the Stack.
MethodUsage
PushInserts an item at the top of the stack.
PeekReturns the top item from the stack.
PopRemoves and returns items from the top of the stack.
ContainsChecks whether an item exists in the stack or not.
ClearRemoves all items from the stack.

Add Values into Stack

The Push() method adds values into the Stack. It allows value of any datatype.
Push() method signature: void Push(object obj);
Example: Push()
Stack myStack = new Stack();
myStack.Push("Hello!!");
myStack.Push(null);
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);

Accessing Stack Elements

You can retrieve stack elements by various ways. Use a foreach statement to iterate the Stack collection and get all the elements in LIFO style.
Example: Access Stack
Stack myStack = new Stack();
myStack.Push("Hello!!");
myStack.Push(null);
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);

foreach (var itm in myStack)
     Console.Write(itm);
Output:






Hello!!

Peek()

The Peek() method returns the last (top-most) value from the stack. Calling Peek() method on empty stack will throw InvalidOperationException. So always check for elements in the stack before retrieving elements using the Peek() method.
Peek() method signature: object Peek();
Example: Stack.Peek()
Stack myStack = new Stack();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);

Console.WriteLine(myStack.Peek());
Console.WriteLine(myStack.Peek());
Console.WriteLine(myStack.Peek());
Output:

5
5

Pop()

You can also retrieve the value using the Pop() method. The Pop() method removes and returns the value that was added last to the Stack. The Pop() method call on an empty stack will raise an InvalidOperationException. So always check for number of elements in stack must be greater than 0 before calling Pop() method.
Pop() signature: object Pop();
Example: Access Stack
Stack myStack = new Stack();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);

Console.Write("Number of elements in Stack: {0}", myStack.Count);

while (myStack.Count > 0)
     Console.WriteLine(myStack.Pop());

Console.Write("Number of elements in Stack: {0}", myStack.Count);
Output:
Number of elements in Stack: 5 





Number of elements in Stack: 0

Contains()

The Contains() method checks whether the specified item exists in a Stack collection or not. It returns true if it exists; otherwise it returns false.
Contains() method signature: bool Contains(object obj);
Example: Stack.Contains()
Stack myStack = new Stack();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);

myStack.Contains(2); // returns true
myStack.Contains(10); // returns false

Clear()

The Clear() method removes all the values from the stack.
Clear() signature: void Clear();
Example: Stack.Contains()
Stack myStack = new Stack();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);
myStack.Push(5);

myStack.Clear(); // removes all elements

Console.Write("Number of elements in Stack: {0}", myStack.Count);
Output:
Number of elements in Stack: 0
 Points to Remember :
  1. Stack stores the values in LIFO (Last in First out) style. The element which is added last will be the element to come out first.
  2. Use the Push() method to add elements into Stack.
  3. The Pop() method returns and removes elements from the top of the Stack. Calling the Pop() method on the empty Stack will throw an exception.
  4. The Peek() method always returns top most element in the Stack.
Visit MSDN for more information on Stack member properties and methods.

C# - Queue

C# includes a Queue collection class in the System.Collection namespace. Queue stores the elements in FIFO style (First In First Out), exactly opposite of the Stack collection. It contains the elements in the order they were added.
Queue collection allows multiple null and duplicate values. Use the Enqueue() method to add values and the Dequeue() method to retrieve the values from the Queue.
C# queue
The following diagram illustrates the Queue class hierarchy.
C# queue
C# Queue

Important Properties and Methods of Queue:

PropertyUsage
CountReturns the total count of elements in the Queue.
MethodUsage
EnqueueAdds an item into the queue.
DequeueRemoves and returns an item from the beginning of the queue.
PeekReturns an first item from the queue
ContainsChecks whether an item is in the queue or not
ClearRemoves all the items from the queue.
TrimToSizeSets the capacity of the queue to the actual number of items in the queue.

Add elements in Queue

Queue is a non-generic collection. So you can add elements of any datatype into a queue using the Enqueue() method.
Enqueue() signature: void Enqueue(object obj)
Example: Enqueue()
Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Access Queue

Dequeue() method is used to retrieve the top most element in a queue collection. Dequeue() removes and returns a first element from a queue because the queue stores elements in FIFO order. Calling Dequeue() method on empty queue will throw InvalidOperation exception. So always check that the total count of a queue is greater than zero before calling the Dequeue() method on a queue.
Dequeue() method signature: object Dequeue()
Example: Dequeue()
Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

while (queue.Count > 0)
    Console.WriteLine(queue.Dequeue());

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);
Output:
Number of elements in the Queue: 4 



Four 
Number of elements in the Queue: 0 

Peek()

The Peek() method always returns the first item from a queue collection without removing it from the queue. Calling Peek() and Dequeue() methods on an empty queue collection will throw a run time exception "InvalidOperationException".
Peek() Method Signature: object Peek();
Example: Peek()
Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

Console.WriteLine(queue.Peek());
Console.WriteLine(queue.Peek());
Console.WriteLine(queue.Peek());

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);
Output:
Number of elements in the Queue: 4 
3
3
3
Number of elements in the Queue: 4
You can iterate a Queue without removing elements of it by converting in to an Array, as below:
Example: Iterate Queue
Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Console.WriteLine("Number of elements in Queue: {0}", queue.Count);

foreach (var i in queue.ToArray())
    Console.WriteLine(i);

Console.WriteLine("Number of elements in Queue: {0}", queue.Count);
Output:
Number of elements in Queue: 4 



Four 
Number of elements in Queue: 4

Contains()

The Contains() method checks whether an item exists in a queue. It returns true if the specified item exists; otherwise it returns false.
Contains() Signature: bool Contains(object obj);
Example: Contains
Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

queue.Contains(2); // true
queue.Contains(100); //false

Clear()

The Clear() method removes all the items from a queue.
Clear() Signature: void Clear();
Example: Clear()
Queue queue = new Queue();
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queue.Enqueue("Four");

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);

queue.Clear();

Console.WriteLine("Number of elements in the Queue: {0}", queue.Count);
Output:
Number of elements in the Queue: 4 
Number of elements in the Queue: 0
Visit MSDN for more information on members of Queue.
 Points to Remember :
  1. The Queue stores the values in FIFO (First in First out) style. The element which is added first will come out First.
  2. Use the Enqueue() method to add elements into Queue
  3. The Dequeue() method returns and removes elements from the beginning of the Queue. Calling the Dequeue() method on an empty queue will throw an exception.
  4. The Peek() method always returns top most element.




No comments:

Post a Comment