C# Collection
C# Collection
What is Collection ??
• In C#, collection represents group of objects.
• System.Collection.Generic is known as
generic collection.
Non-Generic Collection
• ArrayList
• HashTable
• Stack
• Queue
• SortedList
1. ArrayList Class
What is ArrayList ????
• ArrayList is the non-generic type of collection
which is defined in System.Collections
namespace.
class demo
{
public static void Main()
{
ArrayList My_array = new ArrayList();
My_array.Add(12.56);
My_array.Add("Hello SYBCA");
My_array.Add(null);
My_array.Add('G');
My_array.Add(12);
• Add() / AddRange()
• Insert() / InsertRange()
• Remove()
• RemoveAt()
• RemoveRange()
• Sort()
• Reverse()
• Clear()
Adding Elements into ArrayList
Add() and AddRange()
• Use the Add()method to add a single element.
• Syntax :
• int Add(Object value)
• void AddRange(ICollection c)
Example
using System;
using System.Collections; //Adding entire collection using
public class Program ArrayList.AdRange() method.
{ arryList1.AddRange(arryList2);
public static void Main()
{ for(int i = 0; i< arryList1.Count; i++)
ArrayList arryList1 = new ArrayList();
arryList1.Add(1);
Console.WriteLine(arryList1[i]);
arryList1.Add("Two"); }
arryList1.Add(300000); }
arryList1.Add(4.5);
arryList1.Remove(400);
arryList1.RemoveAt(2);
foreach (var item in arryList1)
Console.WriteLine(item);
}
}
Example of RemoveRange()
using System;
using System.Collections;
arryList1.RemoveRange(0,2);
Method Usage
void Clear() Removes all objects from the Stack.
bool Contains(Object value) Return true if value is on the invoking
stack. If value is not found, false returned.
class Demo
{
static public void Main()
{
Stack my_stack = new Stack();
my_stack.Push("Heer");
my_stack.Push("Shree");
my_stack.Push("Swara");
my_stack.Push("Mahi");
}
}
Example of Contains()
using System;
using System.Collections;
class Demo
{
static public void Main()
{
Stack my_stack = new Stack();
my_stack.Push("Heer");
my_stack.Push("Sahaj");
my_stack.Push("Prutha");
my_stack.Push("Rajavi");
if (my_stack.Contains("Heer") == true)
{ Console.WriteLine("Element is found...!!"); }
else
{ Console.WriteLine("Element is not found...!!"); }
}
}
Example of ToArray()
using System;
using System.Collections;
class Demo
{
public static void Main()
{
Stack myStack = new Stack();
myStack.Push("SYBCA-A");
myStack.Push("SYBCA-B");
myStack.Push("TYBCA-A");
myStack.Push("TYBCA-B");
myStack.Push("FYBCA-A");
• When you add an item in the list, it is called enqueue, and when you remove an
item, it is called dequeue .
• Peek returns the oldest element that is at the start of the Queue but does not
remove it from the Queue.
• The capacity of a Queue is the number of elements the Queue can hold.
Example of Enqueue()
using System;
using System.Collections;
class Program
{
static void Main()
{
Queue queue1 = new Queue();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The elements in the queue are:");
foreach(string s in queue1)
{
Console.WriteLine(s);
}
}
}
Example of Dequeue()
using System;
using System.Collections;
class Program
{
static void Main()
{
Queue queue1 = new Queue();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The elements in the queue are:");
foreach(string s in queue1)
{ Console.WriteLine(s); }
queue1.Dequeue();
queue1.Dequeue();
Console.WriteLine("After removal the elements:");
foreach(string s in queue1)
{ Console.WriteLine(s); }
}
}
Example of Contains()
using System;
using System.Collections;
class Program
{
static void Main()
{
Queue queue1 = new Queue();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The elements in the queue are:");
foreach(string s in queue1)
{ Console.WriteLine(s);}
Console.WriteLine("The element MCA is contain in the queue:" + queue1.Contains("MCA"));
Console.WriteLine("The element BCA is contain in the queue:" + queue1.Contains("BCA"));
Console.WriteLine("The element MTech is contain in the queue:" + queue1.Contains("MTech"));
}
}
Example of Clear()
using System;
using System.Collections;
class Program
{
static void Main()
{
Queue queue1 = new Queue();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("The elements in the queue are:" + queue1.Count);
queue1.Clear();
Console.WriteLine("The elements in the queue are :"+queue1.Count);
}
}
Example of Peek()
using System;
using System.Collections;
class Program
{
static void Main()
{
Queue queue1 = new Queue();
queue1.Enqueue("MCA");
queue1.Enqueue("MBA");
queue1.Enqueue("BCA");
queue1.Enqueue("BBA");
Console.WriteLine("Peek the first item from the queue is:" + queue1.Peek());
queue1.Dequeue();
Console.WriteLine("Peek the next item from the queue is:" + queue1.Peek());
}
}
4. HashTable
What is HashTable ??
• A Hashtable is a collection of key/value pairs that are arranged
based on the hash code of the key.
• So instead of storing just one value like the stack, array list and
queue, the hash table stores 2 values. These 2 values form an
element of the hash table.
• The values of each key value pair are ".Net", "C#" and
"ASP.Net" respectively.
Important Points:
• In Hashtable, the key cannot be null, but value can be.
• In hashtable, you can store elements of the same type and of the
different types.
CONSTRUCTOR DESCRIPTION
Hashtable() Initializes a new, empty instance of the Hashtable class
using the default initial capacity, load factor, hash code
provider, and comparer.
4. Next for each key value, we get the associated value in the
hashtable by using the statement ht[k].
Example2
using System;
using System.Collections;
public class Program
{
public static void Main()
{
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***");
h.Add('A', "FYBCA");
h.Add('B', "SYBCA");
h.Add('C', "TYBCA");
PROPERTY DESCRIPTION
Capacity Gets or sets the capacity of a SortedList
object.
Count Gets the number of elements contained in a
SortedList object.
Keys Gets the keys in a SortedList object.
Values Gets the values in a SortedList object.
Example
using System;
using System.Collections; Output : 16
class Demo False
{ False
public static void Main()
{
SortedList mySortedList = new SortedList();
mySortedList.Add("1", "one");
mySortedList.Add("2", "two");
mySortedList.Add("3", "three");
mySortedList.Add("4", "four");
mySortedList.Add("5", "five");
Console.WriteLine(mySortedList.Capacity);
Console.WriteLine(mySortedList.IsFixedSize);
Console.WriteLine(mySortedList.IsReadOnly);
}
}
Method Description
bool ContainsKey(Object) Determines whether a SortedList object contains a
specific key.
class Program
{
static void Main()
{
SortedList slist = new SortedList();
slist.Add(1, "Sunday");
slist.Add(2, "Monday");
slist.Add(3, "Tuesday");
slist.Add(4, "Wednesday");
slist.Add(5, "Thusday");
slist.Add(6, "Friday");
slist.Add(7, "Saturday");
Console.WriteLine("The elements in the SortedList are:");
foreach(DictionaryEntry pair in slist)
{
Console.WriteLine("{0} => {1}", pair.Key, pair.Value);
}
Console.WriteLine("The key 1 contain in SortedList:" + slist.ContainsKey(1));
Console.WriteLine("The key value contain :" + slist.ContainsValue("Monday"));
Console.WriteLine("The key 10 contain in SortedList:" + slist.ContainsKey(10));
Console.WriteLine("The value contain in SortedList:" + slist.ContainsValue("SYBCA"));
}
}
Example of TrimToSize()
using System;
using System.Collections;
class Demo
{
public static void Main()
{
SortedList mylist = new SortedList();
mylist.Add("1", "C++");
mylist.Add("2", "Java");
mylist.Add("3", "DSA");
mylist.Add("4", "Python");
mylist.Add("5", "C#");
mylist.Add("6", "HTML");
Console.WriteLine("Before trimming the capacity is: {0}",mylist.Capacity);
mylist.TrimToSize();
Console.WriteLine("After trimming the capacity is: {0}",mylist.Capacity);
}
}
Example of IndexOfValue() andIndexOfKey()
using System;
using System.Collections;
class Demo
{
static void Main(string[] args)
{
SortedList sortedlist = new SortedList();
sortedlist.Add(1, "Sunday");
sortedlist.Add(2, "Monday");
sortedlist.Add(3, "Tuesday");
sortedlist.Add(4, "Wednesday");
sortedlist.Add(5, "Thusday");
sortedlist.Add(6, "Friday");
sortedlist.Add(7, "Saturday");
//IndexOfKey method
Console.WriteLine("***************INDEXOFKEY***************");
Console.WriteLine();
Console.WriteLine("The index value of the key 4 is:" + sortedlist.IndexOfKey(4));
Console.WriteLine("The index value of the key 1 is:" + sortedlist.IndexOfKey(1));
Console.WriteLine("The index value of the key 7 is:" + sortedlist.IndexOfKey(7));
Console.WriteLine("The index value of the key 2 is:" + sortedlist.IndexOfKey(2));
//IndexofValue method
Console.WriteLine();
Console.WriteLine("***************INDEXOFVALUE***************");
Console.WriteLine();
Console.WriteLine("The index value of the value Sunday is:" + sortedlist.IndexOfValue("Sunday"));
Console.WriteLine("The index value of the value Wednesday is:" +
sortedlist.IndexOfValue("Wednesday"));
Console.WriteLine("The index value of the value Monday is:" +
sortedlist.IndexOfValue("Monday"));
Console.WriteLine("The index value of the value Friday is:" +
sortedlist.IndexOfValue("Friday"));
}
}
Output:
Difference between Stack and Queue
Stack Queue
Objects are inserted and removed at the Objects are inserted and removed from
same end. different ends.
In stacks, the last inserted object is first In queues, the object inserted first is first
to come out. deleted.
Stack operations are called push and Queue operations are called enqueue
pop. and dequeue.