0% found this document useful (0 votes)
52 views77 pages

C# Collection

The document discusses C# collections and ArrayList in particular. It provides the following key points: - ArrayList is a non-generic collection class that represents a dynamic array. It allows storing elements of different types. - ArrayList is defined in the System.Collections namespace. It can be created using constructors that take no parameters or specify initial capacity. Elements can be added, inserted, removed, and accessed by index. - Methods like Add, Insert, Remove, Sort, Reverse and Clear allow manipulating elements in the ArrayList. Properties provide information like count and capacity.

Uploaded by

f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views77 pages

C# Collection

The document discusses C# collections and ArrayList in particular. It provides the following key points: - ArrayList is a non-generic collection class that represents a dynamic array. It allows storing elements of different types. - ArrayList is defined in the System.Collections namespace. It can be created using constructors that take no parameters or specify initial capacity. Elements can be added, inserted, removed, and accessed by index. - Methods like Add, Insert, Remove, Sort, Reverse and Clear allow manipulating elements in the ArrayList. Properties provide information like count and capacity.

Uploaded by

f
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 77

C# Collections

What is Collection ??
• In C#, collection represents group of objects.

• Collections are specialized classes for data storage and


retrieval.

• These classes provide support for ArrayList, Stack,


Queues, Hashtable and SortedList.

• The main usage of Collections is that they standardize the


way of which the objects are handled by your program.
• .Net Framework supports four general types
of collections: non-generic, specialized, bit
based and generic.

• System.Collection namespace is known as


non-generic collection.

• 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.

• It is used to create a dynamic array means the


size of the array is increase or decrease
automatically according to the requirement of
your program, there is no need to specify the
size of the ArrayList.
• Or in other words, ArrayList represents an ordered
collection of an object that can be indexed
individually.
• In ArrayList, you can store elements of the same type
and of the different types.
• It belongs to the non-generic collection.
• The ArrayList is defined under System.Collections
namespace.
• So, when you use Arraylist in your program you must
add System.Collections namespace.
• You cannot implement a multi-dimensional array
using ArrayList.
• The capacity of an ArrayList is the number of elements
the ArrayList can hold.

• You are allowed to use duplicate elements in your


ArrayList.

• You can apply searching and sorting on the elements


present in the ArrayList.

• Arraylist can accept null as a valid value.

• ArrayList can be accessed using foreach or for loop or


indexer.
How to create the ArrayList?
• ArrayList class has three constructors which are used to create the
ArrayList.

• ArrayList(): This constructor is used to create an instance of


ArrayList class which is empty and having no initial capacity.

• ArrayList(Int32): This constructor is used to create an instance of


ArrayList class which is empty and having the specified initial
capacity.

• ArrayList(ICollection): This constructor is used to create an array list


initialized with the elements from the specified collection and
having the same initial capacity which is copied from collection.
• Let’s see how to create an ArrayList using ArrayList()
constructor:
• Step 1: Include System.Collections namespace in your program
with the help of using keyword. Syntax:
• using System.Collections;

• Step 2: Create an ArrayList using ArrayList class as shown


below:
• ArrayList list_name = new ArrayList();

• Step 3: If you want to add elements in your ArrayList then use


Add() method to add elements in your ArrayList.

• Step 4: The elements of the ArrayList is accessed by using a


foreach loop, or by for loop, or by indexer.
Example 1:
using System;
using System.Collections;

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

foreach(var elements in My_array)


{
Console.WriteLine(elements);
}
}
}
• Methods and Properties of ArrayList Class
• The following table lists some of the commonly used properties of the
ArrayList class −
Example :
using System;
using System.Collections;
class demo
{
public static void Main()
{
ArrayList myList = new ArrayList();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add(4);
myList.Add(5);

Console.WriteLine("Number of elements: " + myList.Count);


Console.WriteLine("Current capacity: " + myList.Capacity);
Console.WriteLine("Is fix size :" + myList.IsFixedSize);
Console.WriteLine("Is read only :" + myList.IsReadOnly);
}
}
The following table lists some of the commonly used
methods of the ArrayList class −

• 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.

• The AddRange() method to add multiple elements


from the other collections into an ArrayList.

• 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);

ArrayList arryList2 = new ArrayList()


{100,200};
Inserting Elements into ArrayList
Insert() and 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.

• Syntax: void Insert(int index, Object value)


void InsertRange(int index, ICollection c)
Example of Insert()
using System;
using System.Collections;
myArryList.Insert(1,"Second
Item");
public class Program
{
myArryList.Insert(2, 100);
public static void Main()
{
ArrayList myArryList = new ArrayList(); foreach (var val in
myArryList.Add(1); myArryList)
myArryList.Add("Two");
myArryList.Add(3);
Console.WriteLine(val);
myArryList.Add(4.5); }
}
Example of InsertRange()
using System; ArrayList arryList2 = new
using System.Collections; ArrayList();
arryList2.Add(10);
public class Program arryList2.Add(20);
{ arryList2.Add(30);
public static void Main()
{ arryList2.InsertRange(2,arryList1);
ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
foreach(var item in arryList2)
arryList1.Add(200);
Console.WriteLine(item);
}
}
Remove Elements from ArrayList
• Remove:This method is used to remove the first
occurrence of a specific object from the
ArrayList.
– void Remove(Object obj)
• RemoveAt: This method is used to remove the
element at the specified index of the ArrayList.
– void RemoveAt(int index)
• RemoveRange: This method is used to remove
a range of elements from the ArrayList.
– void RemoveRange(int index, int count)
Example of Remove() and RemoveAt()
using System;
using System.Collections;
public class Program
{
public static void Main()
{
IList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);
arryList1.Add(400);

arryList1.Remove(400);
arryList1.RemoveAt(2);
foreach (var item in arryList1)
Console.WriteLine(item);
}
}
Example of RemoveRange()
using System;
using System.Collections;

public class Program


{
public static void Main()
{
ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);

arryList1.RemoveRange(0,2);

foreach (var item in arryList1)


Console.WriteLine(item);
}
}
Reverse and sort the order of the elements
in the entire ArrayList

• Reverses the order of the elements in the


entire ArrayList.
– void Reverse ()

• Sorts entire elements of the ArrayList.


– void Sort ()
Example of Sort(), Reverse()
using System;
using System.Collections;
arryList1.Reverse();
public class Program Console.WriteLine("Reverse Order:");
{
public static void Main()
{
foreach(var item in arryList1)
ArrayList arryList1 = new ArrayList(); Console.WriteLine(item);
arryList1.Add(300);
arryList1.Add(200);
arryList1.Add(100);
arryList1.Sort();
arryList1.Add(500);
arryList1.Add(400); Console.WriteLine("Ascending
Order:");
Console.WriteLine("Original Order:");

foreach(var item in arryList1) foreach(var item in arryList1)


Console.WriteLine(item);
Console.WriteLine(item);
}
}
Output
Remove all elements from the
ArrayList
• Removes all the elements in ArrayList.
– void Clear ()
Example of Clear()
using System;
using System.Collections;
class Demo
{
public static void Main()
{
ArrayList myList = new ArrayList(10);
myList.Add("A");
myList.Add("B");
myList.Add("C");
myList.Add("D");
myList.Add("E");
myList.Add("F");

Console.WriteLine("Number of elements in ArrayList initially : " + myList.Count);


myList.Clear();
Console.WriteLine("Number of elements in ArrayList : " + myList.Count);
}
}
2. Stack Class
What is Stack ???
• Stack represents a last-in, first out
collection of object.

• It is used when you need a last-in, first-


out access to items.

• When you add an item in the list, it is


called pushing the item and when you
remove it, it is called popping the item.

• This class comes under


System.Collections namespace.
• The capacity of a Stack is the number of
elements the Stack can hold.

• As elements are added to a Stack, the capacity


is automatically increased as required through
reallocation.

• Stack accepts null as a valid value and allows


duplicate elements.
• Constructor
• Important Properties and Methods of Stack:
Property Usage
Count Returns the total count of elements in the
Stack.

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.

object Peek() Returns the object at the top of the Stack


without removing it.

object Pop() Removes and returns the object at the top


of the Stack.

void Push(Object value) Inserts an object at the top of the Stack.


object[] ToArray() Copies the Stack to a new array.
How to create a Stack?
• Step 1: Include System.Collections namespace in
your program with the help of using keyword.
– using System.Collections;

• Step 2: Create a stack using Stack class as shown


below:
– Stack stack_name = new Stack();

• Step 3: If you want to add elements in your stack,


then use Push() method to add elements in your
stack.
Example of Push()
using System;
using System.Collections;
class Demo
{
static public void Main()
{
Stack my_stack = new Stack();
my_stack.Push("Shreeja");
my_stack.Push("Hello SYBCA...");
my_stack.Push('A');
my_stack.Push(null);
my_stack.Push(1234);
my_stack.Push(490.98);
foreach(var elem in my_stack)
{
Console.WriteLine(elem);
}
}
}
How to remove elements from the Stack?
• In Stack, you are allowed to remove elements from the stack.

• The Stack class provides two different methods to remove


elements and the methods are:

• Clear: This method is used to remove all the objects from


the stack.

• Pop: This method removes the beginning element of the


stack.
Example of Pop() and Clear()
using System;
using System.Collections;
class Demo
{
static public void Main()
{
Stack my_stack = new Stack();
my_stack.Push("Welcome");
my_stack.Push("Good Morning");
my_stack.Push("How r you!");
my_stack.Push("SYBCA");
Console.WriteLine("Total elements is:"+my_stack.Count);
foreach(var a in my_stack)
{Console.WriteLine(a);}
my_stack.Pop();
Console.WriteLine("Total elements is:"+my_stack.Count);
foreach(var a in my_stack)
{Console.WriteLine(a);}
my_stack.Clear();
Console.WriteLine("Total elements is:"+my_stack.Count);
}
}
Example of Peek()
using System;
using System.Collections;

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");

Console.WriteLine("Total elements present in: "+ my_stack.Count);


Console.WriteLine("Topmost element of my_stack: "+ my_stack.Pop());
Console.WriteLine("Total elements present in: "+ my_stack.Count);
Console.WriteLine("Topmost element of my_stack: "+ my_stack.Peek());
Console.WriteLine("Total elements present: "+ my_stack.Count);

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

Object[] arr = myStack.ToArray();

foreach(Object str in arr)


{Console.WriteLine(str);}
}
}
3. Queue Class
What is Queue ??
• Queue represents a first-in, first out collection of object.

• It is used when you need a first-in, first-out access of items.

• When you add an item in the list, it is called enqueue, and when you remove an
item, it is called dequeue .

• This class comes under System.Collections namespace and implements ICollection,


IEnumerable, and ICloneable interfaces.

• 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.

• It uses the key to access the elements in the collection.

• 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.

• It is the non-generic type of collection which is defined


in System.Collections namespace.
• Below are some example of how values of a hash table
might look like.
– { "001" , ".Net" }
– { "002" , ".C#" }
– { "003" , "ASP.Net" }

• Above we have 3 key value pairs.

• The keys of each element are 001, 002 and 003


respectively.

• 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.

• The capacity of a Hashtable is the number of elements that


Hashtable can hold.

• The Hashtable class implements


the IDictionary, ICollection, IEnumerable, ISerializable, IDeserializa
tionCallback, and ICloneable interfaces.

• In hashtable, you can store elements of the same type and of the
different types.

• In Hashtable, key must be unique. Duplicate keys are not allowed.


How to create HashTable ?
• Hashtable class provides 16 different types of
constructors which are used to create a hashtable,
here we only use Hashtable() constructor. 

CONSTRUCTOR DESCRIPTION
Hashtable() Initializes a new, empty instance of the Hashtable class
using the default initial capacity, load factor, hash code
provider, and comparer.

Hashtable(IDictionary) Initializes a new instance of the Hashtable class by


copying the elements from the specified dictionary to the
new Hashtable object

Hashtable(int capacity) Initializes a new ,empty instance of the HashTable class


using the specific initial capacity.
• Step 1: Include System.Collections namespace in
your program with the help of using keyword:
– using System.Collections;

• Step 2: Create a hashtable using Hashtable class as


shown below:
– Hashtable hashtable_name = new Hashtable();

• Step 3: If you want to add a key/value pair in your


hashtable, then use Add() method to add elements
in your hashtable. And you can also store a
key/value pair in your hashtable without
using Add() method.
– hashtable.add("key","value")
Example
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable ht = new Hashtable();
ht.Add("001",".Net");
ht.Add("002","C#");
ht.Add("003","ASP.Net");

ICollection keys = ht.Keys;

foreach (String k in keys)


{
Console.WriteLine(ht[k]);
}
}
}
1. First, we declare the hashtable variable using the Hashtable
data type by using keyword "New." The name of the variable
defines is 'ht'.

2. We then add elements to the hash table using the Add


method. Remember that we need to add both a key and
value element when adding something to the hashtable.

3. There is no direct way to display the elements of a hash table.


1. In order to display the hashtable , we first need to get the list of
keys (001, 002 and 003) from the hash table.
2. This is done via the ICollection interface. This is a special data type
which can be used to store the keys of a hashtable collections. We
then assign the keys of the hashtable collection to the variable
'keys'.

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***");

foreach (var value in ht.Values)


Console.WriteLine("Value:{0}", value);
}
}
Example of Contains(),ContainsKey()
and ContainsValue()
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable ht = new Hashtable();
ht.Add("001",".Net");
ht.Add("002","C#");
ht.Add("003","ASP.Net");
Console.WriteLine(ht.Contains("002"));
Console.WriteLine(ht.ContainsKey("001"));
Console.WriteLine(ht.ContainsValue("C#"));
}
}
Example of Remove()
using System;
using System.Collections;
public class Program
{
public static void Main()
{
Hashtable ht = new Hashtable()
{
{ 1, "One" },
{ 2, "Two" },
{ 3, "Three" },
{ 4, "Four" },
{ "Fv", "Five" },
{ 8.5F, 8.5 }
};
ht.Remove("Fv");
foreach (var key in ht.Keys )
Console.WriteLine("Key:{0}, Value:{1}",key , ht[key]);
}
}
Example of Clear()
using System;
using System.Collections;
class Program
{
static void Main()
{
Hashtable ht = new Hashtable();
ht.Add("001",".Net");
ht.Add("002","C#");
ht.Add("003","ASP.Net");

ICollection keys = ht.Keys;

foreach (String k in keys)


{
Console.WriteLine(ht[k]);
}
ht.Clear(); // removes all elements
Console.WriteLine("Total Elements: {0}", ht.Count);
}
}
Example of GetHash()
using System;
using System.Collections;

class HashCode : Hashtable


{
static void Main()
{
HashCode h = new HashCode();

h.Add('A', "FYBCA");
h.Add('B', "SYBCA");
h.Add('C', "TYBCA");

ICollection Key = h.Keys;


int hcode = h.GetHash('C');
Console.Write("HashCode: " + hcode);
}
}
5. SortedList Class
What is SortedList ??
• SortedList class is a collection of (key, value) pairs which are sorted according
to keys.

• Those pairs can be accessible by key and as well as by index(zero-based


indexing).

• A SortedList object internally maintains two arrays to store the elements of


the list, i.e, one array for the keys and another array for the associated values.

• A key cannot be null, but a value can be.

• This comes under System.Collections namespace.


• The capacity of a SortedList object is the number of
elements the SortedList can hold.

• A SortedList does not allow duplicate keys.

• Operations on a SortedList object tend to be slower


than operations on a Hashtable object because of
the sorting.

• Elements in this collection can be accessed using


an integer index.
CONSTRUCTOR DESCRIPTION
SortedList() Initializes a new instance of the SortedList class that
is empty.
SortedList(IDictionary) Initializes a new instance of the SortedList class that
contains elements copied from the specified
dictionary.
SortedList(Int32) Initializes a new instance of the SortedList class that
is empty, has the specified initial capacity.
SortedList(IComparer) Initializes a new instance of the SortedList class that
is empty, has the default initial capacity, and is
sorted according to the specified IComparer
interface.

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.

bool ContainsValue(Object) Determines whether a SortedList object contains a


specific value.
object GetByIndex(int Index) Gets the value at the specified index of a SortedList
object.

object GetKey(Int32) Gets the key at the specified index of a SortedList


object.

GetKeyList() Gets the keys in a SortedList object.


GetValueList() Gets the values in a SortedList object.
IndexOfKey(Object) Returns the zero-based index of the specified key in a
SortedList object.

IndexOfValue(Object) Returns the zero-based index of the first occurrence of


the specified value in a SortedList object.
Example of ContainsKey() and
ContainsValue()
using System;
using System.Collections;

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

LIFO (Last in First out) FIFO (First in First out)

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.

Stacks are visualized as vertical Queues are visualized as horizontal


collections. collections.

Collection of dinner plates at a wedding People standing in a file to board a bus is


reception is an example of stack. an example of queue.

You might also like