C#_Collection_Generic_GenericCollection-Part 1
C#_Collection_Generic_GenericCollection-Part 1
So, we should know in advance how many elements are going to be stored in the array.
Once the array is created, then we can never increase the size of an array.
If you want then we can do it manually by creating a new array and copying the old array
elements into the new array.
As the array size is fixed, if we allocate more memory than the requirement then the extra
memory will be wasted.
On the other hand, if we allocate less memory than the requirement, then it will create the
problem.
So insertions and deletions of array elements are very difficult and also time-consuming.
It is also not possible to delete or remove elements from the middle of an array.
To overcome the above problems Array Class and Collections are introduced in C#.
Collection
Collection is a data structure which allows to store the element in a specialized way.
C# collection types are designed to store, manage and manipulate similar data more efficiently. Data
manipulation includes adding, removing, finding, and inserting data in the collection. Collection types
implement the following common functionality:
Replacing items
Copy and clone collections and items
Capacity and Count properties to find the capacity of the collection and number of items in the
collection
.NET supports two types of collections, generic collections and non-generic collections. Prior to .NET 2.0,
it was just collections and when generics were added to .NET, generics collections were added as well.
Generic collections with work generic data type. Learn more about generics here: Generics in C#.
Non-generic Generic
1. Non-Generic
In non-generic collections, each element can represent a value of a different type. The collection size is
not fixed. Items from the collection can be added or removed at runtime.
ArrayList
Hashtable
Sortedlist
Stack
Queue
9999
1 ArrayList
It is basically an alternative to an array. However, unlike array you can add and remove items from a list
at a specified position using an
index and the array resizes itself automatically. It also allows dynamic memory allocation, adding,
searching and sorting items in the list.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace BasicCSharpConcept
{
class CollectionsDemo
{
list1.Add(59);
list1.Add(57);
list1.Add(55);
list1.Add(53);
list1.Add(51);
// Console.ReadLine();
Console.WriteLine("Elements After Adding New List to Array List");
list.AddRange(list1);
Console.ReadLine();
if (list.Contains("String2"))
{
Console.WriteLine("Elements is Available at Index No {0}", list.IndexOf("String1"));
}
else
{
Console.WriteLine("Elements is Not Available in the List");
}
list.RemoveRange(index, 3);
Console.WriteLine("Elements Removing Adding list1 to Array List");
//list.RemoveRange(list1);
foreach (var l in list)
{
Console.WriteLine(l);
}
list.Remove("String1");
Console.WriteLine("Elements Removing String1 from Array List");
//list.RemoveRange(list1);
foreach (var l in list)
{
Console.WriteLine(l);
}
list.RemoveAt(1);
list.Remove('C');
Console.ReadLine();
Console.WriteLine("Elements Removing Element at 1 Index from Array List");
//list.RemoveRange(list1);
Console.WriteLine("before Sorting Elements");
foreach (var l in list)
{
Console.WriteLine(l);
}
int count=list.Count;
Console.WriteLine("Number of Elements are {0} Before Clearing All Elements",count);
list.Clear();
int count1 = list.Count;
Console.WriteLine("Number of Elements are {0} After Clearing All Elements", count1);
list.Insert(0, 100);
list.Insert(1, 200);
Console.WriteLine("Number of Elements are {0} After Inserting", count);
foreach (var l in list)
{
Console.WriteLine(l);
}
list.InsertRange(1,list1);
Console.WriteLine(" After Inserting the Range");
foreach (var l in list)
{
Console.WriteLine(l);
}
}
}
}
2 Hashtable
A hash table is used when you need to access elements by using key, and you can identify a useful key
value. Each item in the hash table has a key/value pair. The key is used to access the items in the
collection.
//Console.WriteLine("Only Values");
//foreach (string v in value)
//{
// Console.WriteLine(": " + v);
//}
//bool k = ht.ContainsKey("008");
//if (k == true)
//{
// Console.WriteLine("Key is available in the list with value {0} ", ht["007"]);
// }
//else
//{
// Console.WriteLine("Key is not available in the list");
//}
//string v3= "Mausam Benazir Nur";
//bool v1 = ht.ContainsValue("Mausam Benazir Nur");
ht.Remove("007");
Console.WriteLine("After Removing 007 from List");
3 SortedList
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionDemo
{
public class SortedListDemo
{
public void SortedListDemo1()
{
SortedList sortedList = new SortedList();
sortedList.Add(600, "Suresh");
sortedList.Add(200, "Anil");
sortedList.Add(400, "Vinit");
sortedList.Add(500, "Rajesh");
sortedList.Add(100, "Sunil");
sortedList.Add(300, "Punit");
Console.WriteLine(sortedList.GetByIndex(i));
}
Console.WriteLine("\n\n\n");
Console.WriteLine("Elements Through Key Number");
Console.WriteLine(sortedList.GetKey(i));
}
}
}
}
4 Stack
Returns the object at the top of the Stack without removing it.
class Program {
static void Main(string[] args) {
Stack st = new Stack();
st.Push('A');
st.Push('M');
st.Push('G');
st.Push('W');
st.Push('V');
st.Push('H');
Console.WriteLine("The next poppable value in stack: {0}", st.Peek());
Console.WriteLine("Current stack: ");
Console.WriteLine();
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.
5 Queue
It is used when you need a first-in, first-out access of items. When you add an item in the list, it is call
1) public virtual void Clear();
using System;
using System.Collections;
namespace CollectionsApplication {
class Program {
static void Main(string[] args) {
Queue q = new Queue();
q.Enqueue('A');
q.Enqueue('M');
q.Enqueue('G');
q.Enqueue('W');
Console.WriteLine();
q.Enqueue('V');
q.Enqueue('H');
Console.WriteLine("Current queue: ");
foreach (char c in q) Console.Write(c + " ");
Console.WriteLine();
Console.WriteLine("Removing some values ");
char ch = (char)q.Dequeue();//Unboxing
Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();F
Console.WriteLine("The removed value: {0}", ch);
Console.ReadKey();
}
}
}
Boxing : Process of converting value type Eg Char or int type to the reference types is know as boxing
int i=10;
object o=i;//boxing it is done automatically no casting is required
UnBoxing : process of Converting reference type Eg Stack or Queue to Value type i.e int or char is know
unboxing
object o=10;
int i=(int)o;//unboxing it has to be casting by using cast operator “()”;