1. What is Collection-
Ans: Collection is group of object that holds many value in specific series is called collection.
Two Types of Collection:
1. Generic Collection
2. Non- Generic Collection.
1. Non-Generic Collection:
1. Array List 2.Has table 3. sorted list 4. Stack 5. Queue
Generic List:
1. List 2. Dictionary 3. Stack 4. Queue
1. What is Array List:
Ans: Array list whose size are dynamically increased.
Namespace: System.collection
1.We can store same type of data in array but Array list based on the object so we can store any type of data whether it is int, string, double or any custom data type.
Hash table:
It is collection of Key value and pair.
Namespace: System.Collection. It is Similar to generic Dictionary.
Difference b/w Hash table and Dictionary.
Hash table and Dictionary are collection of data structure hold data in the form of key value and pair.
1. Hash table is not generic type. while dictionary is generic type.
2. Hash table is Thread safe while Dictionary is not.
3. Dictionary is type safe means value need not be boxing. while hash table need boxing and Un boxing.
4.In Dictionary, when you try to access the value of key which is not exist in the collection. it through error Key not find. while hash table returns null value.
5. when using large collection of key value pair hash table is more Efficient then dictionary.
6. When we retrieve the record in collection the hashtable does not maintain the order of entries while dictionary maintains the order of entries by which entries were added.
Difference between Array and Array List
1. Array is a strongly type it contains only specific type of items/element.
but Arraylist can store any types of items.
2. In array we can store only one type of datatypes either int,string,float.
but array list can store all types of data.
3. array belongs to the system.array namespace. while array list belongs to the system.collection
namespace
4.array size was fixed but array list is dynamic size.
Collection:
* Collection is group of object that can help with store, manipulate the date more flexible way.
Two types of collection
1. Generic 2. non-generic
1. Generic
List
Stack
Queue
LinkedList
HashSet
SortedSet
Dictionary
SortedDictionary
SortedList
2. Non-Generic
ArrayList
Stack
Queue
Hashtable
===================================
List:
C# List<T> class is used to store and fetch elements. It can have duplicate elements.
It is found in System.Collections.
......................................................................................
HashSet:
C# HashSet class can be used to store, remove or view elements.
It does not store duplicate elements.
It is suggested to use HashSet class if you have to store only unique elements.
It is found in System.Collections.Generic namespace.
SortedSet:
C# SortedSet class can be used to store, remove or view elements.
It maintains ascending order and does not store duplicate elements.
It is suggested to use SortedSet class if you have to store unique elements and maintain ascending order.
It is found in System.Collections.Generic namespace.
=======================================================================================================
Stack<T>
C# Stack<T> class is used to push and pop elements.
It uses the concept of Stack that arranges elements in LIFO (Last In First Out) order.
It can have duplicate elements. It is found in System.Collections.Generic namespace
Example:
public static void Main(string[] args)
{
Stack<string> names = new Stack<string>();
names.Push("Sonoo");
names.Push("Peter");
names.Push("James");
names.Push("Ratan");
names.Push("Irfan");
}
=============================================================================
C# Queue<T>
C# Queue<T> class is used to Enqueue and Dequeue elements.
It uses the concept of Queue that arranges elements in FIFO (First In First Out) order.
It can have duplicate elements. It is found in System.Collections.Generic namespace.
====================================================================================
C# Dictionary<TKey, TValue>
C# Dictionary<TKey, TValue> class uses the concept of hashtable.
It stores values on the basis of key. It contains unique keys only. By the help of key, we can easily search or remove elements.
It is found in System.Collections.Generic namespace.
====================================================================================================