7.collection Classes
7.collection Classes
Chapter (6)
Collection Classes
1
Contents
Array Class
Properties and Methods in Array Class Hashtable
Properties and Method in Hashtable
Collection Classes
Differences between SortedList and
ArrayList Hashtable
Properties and Methods in ArrayList
Stack
List Properties and Method in Stack
Properties and Methods in Dictionary
Queue
Dictionary Properties and Method in Queue
Properties and Methods in Dictionary
SortedList
Properties and Method in SortedList
2
Array Class
store multiple values in a single variable, instead of declaring separate variables for each value
stores a fixed-size sequential collection of elements of the same type
two kinds of arrays in C#
Normal Array:
3
Array Class
another kind of array provided by Array class
the base class for all the arrays in C#
defined in the System namespace
Example
4
Properties and Methods in Array Class
Table 1
Property Description
IsFixedSize Gets a value indicating whether the Array has a fixed size
Length Gets a 32-bit integer that represent the total number of elements in all the dimensions of the
Array
LongLength Gets a 64-bit integer that represent the total number of elements in all the dimensions of the
Array
5
Properties and Methods in Array Class
Table 2
Method Description
Clear( ) Sets a range of elements in the Array to zero, to false, or to null, depending on the
element type
Copy(Array, Array,Int32) Copies a range of elements from an Array starting at the first element and pastes them
into another Array starting at the first element. The length is specified as a 32-bit
integer.
CopyTo(Array, Int32) Copies all the elements of the current one-dimensional Array to the specified one-
dimensional Array starting at the specified destination Array index. The index is
specified as a 32-bit integer.
GetLength(dimension) Gets a 32-bit integer that represents the number of elements in the specified dimension of
the Array
GetLongLength(dimension) Gets a 64-bit integer that represents the number of elements in the specified dimension of
the Array
GetLowerBound(dimension) Gets the lower bound of the specified dimensions in the Array
6
Properties and Methods in Array Class
Table 3
Method Description
GetUpperBound(dimension) Gets the upper bound of the specified dimension in the Array. The index is specified as a 32-bit
integer.
GetValue(Int32) Gets the value at the specified position in the one-dimensional Array. The index is specified as
32-bit integer
IndexOf(Array, Object) Searches for the specified object and returns the index of the first occurrence within the entire
one-dimensional Array
Reverse(Array) Reverse the sequence of the elements in the entire one-dimensional Array
SetValue(Object, Int32) Sets a value to the element at the specified position in the one-dimensional Array. The index is
specified as 32-bit integer
Sort(Array) Sorts the elements in an entire one-dimensional Array using the IComparable implementation of
each element of the Array
ToString( ) Returns a string that represents the current object. (Inherited from Object)
7
Collection Classes
store, manage and manipulate (add, remove, find, and insert) similar data more efficiently
8
Collection Classes
ArrayList List
HashTable Dictionary
SortedList SortedList
Stack Stack
Queue Queue
9
ArrayList
It is similar to an array, except that it grows automatically as you add items in it.
10
Properties and Methods in ArrayList
Table 4
Property Description
Capacity Gets or sets the number of elements that the ArrayList can contain.
IsFixedSize Gets a value indicating whether the ArrayList has a fixed size.
11
Properties and Methods in ArrayList
Table 5
Property Description
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.
12
Properties and Methods in ArrayList
Table 6
Property Description
Contains( ) Checks whether specified element exists in the ArrayList or not. Returns true if exists
otherwise false.
Clear( ) Removes all the elements in ArrayList.
GetRange( ) Returns specified number of elements from specified index from ArrayList.
IndexOf( ) Search specified element and returns zero based index if found. Returns -1 if element
not found.
ToArray( ) Returns compitible array from an ArrayList.
13
Properties and Methods in ArrayList
14
Properties and Methods in ArrayList
Accessing Elements
Example
ArrayList A = new ArrayList( );
A.Add(1);
A.Add("Two");
A.Add(3);
A.Add(4.5f);
Console.WriteLine(value);
( or )
Console.WriteLine(A[i]);
16
Properties and Methods in ArrayList
Example:
ArrayList A = new ArrayList ();
A.Add(1);
A.Add("Two");
A.Add(3);
A.Add(4.5);
Example:
ArrayList A = new ArrayList();
A.Add(100);
A.Add(200);
B.InsertRange(2, A);
foreach(var item in B)
Console.WriteLine(item); 18
Properties and Methods in ArrayList
Remove Elements from ArrayList
Example:
ArrayList num = new ArrayList();
num.Add(100);
num.Add(200);
num.Add(300);
num.Remove(100);
foreach (var item in num)
Console.WriteLine(item);
19
Properties and Methods in ArrayList
Example: RemoveAt()
ArrayList num = new ArrayList();
num.Add(100);
num.Add(200);
num.Add(300);
num.RemoveAt(1);
20
Properties and Methods in ArrayList
Example:
ArrayList num = new ArrayList();
num.Add(100);
num.Add(200);
num.Add(300);
num.RemoveRange(0,2);
21
Properties and Methods in ArrayList
22
Properties and Methods in ArrayList
23
List
24
Properties and Methods in List
Table 7
Properties/ Methods Description
Capacity Gets or sets the total number of elements the internal data structure can
hold without resizing.
BinarySearch( ) Uses a binary search algorithm to locate a specific element in the sorted
List<T> or a portion of it.
collection of Keys and Values, where key is like word and value is like definition
27
Properties and Methods in Dictionary
Table 8
Property Description
Count Gets the total number of elements exists in the Dictionary<Tkey, Tvalue>
28
Properties and Methods in Dictionary
Table 9
Method Description
Remove( ) Removes the first occurrence of specified item from the Dictionary<TKey, TValue>
TryGetValue( ) Returns true and assigns the value with the specified key, if key does not exist
29
Properties and Methods in Dictionary
Create Dictionary and Add elements
30
Properties and Methods in Dictionary
Create Dictionary with initialization
31
Properties and Methods in Dictionary
Accessing elements in Dictionary by using for loop
32
Properties and Methods in Dictionary
Accessing Values in Dictionary
33
Properties and Methods in Dictionary
Removing elements in Dictionary
dict.Remove(2);
dict.Remove(7);
dict.Clear();
if ( dict.ContainsValue(“pink”) )
Console.WriteLine("This value already exist");
34
Properties and Methods in Dictionary
if (dict.ContainsKey(3))
Console.WriteLine("This key already exist");
35
SortedList
36
Properties and Methods in SortedList
Table 10
Property Description
Capacity Gets or sets the number of elements that the SortedList instance can store
IsFixedSize Gets a value indicating whether the SortedList has a fixed size
37
Properties and Methods in SortedList
Table 11
Method Description
GetKey(int index) Returns the key stored at specified index in internal array
38
Properties and Methods in SortedList
Create SortedList and Add elements
SortedList s = new SortedList( );
s.Add(3, “Red");
s.Add(4, “Pink");
s.Add(1, “Cyan");
s.Add(5, null);
s.Add(2, true);
IsReadOnly, IsFixedSize
Console.WriteLine("IsReadOnly: " + s.IsReadOnly);
Console.WriteLine("IsFixedSize: " + s.IsFixedSize);
40
Properties and Methods in SortedList
41
Properties and Methods in SortedList
Copy SortedList
SortedList s1=(SortedList) s.Clone(); // create a shallow copy of sortedlist object
// explicit conversion from object type to SortedList
46
Hashtable
non-generic collection type
stores key-value pairs of any datatype where the Key must be unique
47
Properties and Methods in Hashtable
Table 12
Property Description
48
Properties and Methods in Hashtable
Table 13
Method Description
Add Adds an item with a key and value into hashtable
Remove Removes the item with the specified key from hashtable
49
Properties and Methods in Hashtable
51
Properties and Methods in Hashtable
Console.WriteLine(value1);
Console.WriteLine(value2);
Console.WriteLine(value3);
52
Properties and Methods in Hashtable
53
Properties and Methods in Hashtable
54
Properties and Methods in Hashtable
if(ht.ContainsKey(1))
Console.WriteLine(“Key ready exist”);
55
Properties and Methods in Hashtable
Copy Hashtable
Hashtable h =(Hashtable) ht.Clone(); // convert object type to hashtable typem
// shallow copy, return object type
Array A = Array.CreateInstance(typeof(object),4);
ht.CopyTo(A,0);
SoredList Hashtable
a collection of key/value pairs that are sorted by the keys a collection of key/value pairs that are organized based on
the hash code of the key
elements are accessible by key and by index elements are accessible by key
57
Stack
C# includes a special type of collection which stores elements in LIFO style (Last In First Out).
58
Properties and Methods in Stack
Table 15
Properties/Methods Description
59
Properties and Methods in Stack
60
Properties and Methods in Stack
Console.WriteLine(myStack.Peek());
Console.WriteLine(myStack.Peek());
Console.WriteLine(myStack.Peek());
61
Queue
Queue stores the elements in FIFO style (First In First Out), exactly opposite of the Stack collection.
62
Properties and Methods in Queue
Table 16
Properties/Methods Description
Dequeue( ) Removes and returns an item from the beginning of the queue.
TrimToSize( ) Sets the capacity of the queue to the actual number of items in the
queue.
63
Properties and Methods in Queue
64
Properties and Methods in Queue
Console.WriteLine(q.Peek());
Console.WriteLine(q.Peek());
Console.WriteLine(q.Peek());
65
Thank You
66