What is Collection in.NET
Microsoft .NET provides lots of collection data types that can store a set of records or values. there are two types of collections introduced by Microsoft in C#.NET, those are generic collections and non-generic collections. Generic collections are type-safe at compile time. so that it provides much better performance as compared to the non-generic collection. It supports a type parameter when they are constructed and do not require to do boxing-unboxing or type-conversation from the Object type when you add or remove items from the collection. Non-generic collections store items as Objects require casting, and most are not supported for Windows Store app development. However, you may see non-generic collections in older codes. by most developers, it is recommended to use the generic collection, because it is faster and safer also, there is less chance of exception and compile-time error with it.
to use a non-generic collection in your code need to use the namespace System.Collections, and to use generic collection need to use the namespace System.Collections.Generic
Generic Collections Data Types
The term Generics means the general, not a specific, in another word, generic type is a type to maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. the namespace uses to call the generic in your class is System.Collections.Generic
T represent generics, it works as an encapsulation that encapsulates datatype
Example
List<T> is a generic collection that can be declared and used with any type, such as List<int>, List<string>, or List<GenericClass>.
How to define generics
The Generics can be anything it can class, structures, interfaces, and methods that have parameters. below code, snippet shows the example of it
public class GenericClass<T>
{
public T Field;
}
If we talk about the instantiation of a generic class or method, we need to specify the actual type which we pass over there as a parameter. so in the below code snippet, you can see, how the actual data type is passed at the place of T
public static void Main()
{
GenericClass<string> instanceObject = new GenericClass<string>();
g.Field = "This is my string";
Console.WriteLine("My Generic Field = \"{0}\"", instanceObjectField);
Console.WriteLine("Generic.Field.GetType() = {0}",
instanceObject.Field.GetType().FullName);
}
Below is the list of generic collection
- List<T>
- Dictionary<TKey,TValue>
- SortedList<TKey,TValue>
- Queue<T>
- Stack<T>
- LinkedList<T>
- ObservableCollection<T>
- Hashset<T>
- SortedSet<T>
List<T>
List<T> is the generic collection that contains elements of the specified type. It grows automatically as you add elements to it, supports all data types and class objects.
Dictionary<TKey,TValue>
Dictionary<TKey,TValue> contains generic key-value pairs.
SortedList<TKey,TValue>
SortedList is a great example of generics it stores key and value pairs. It has a mechanism to add the elements in ascending order of key by default.
Queue<T>
Queue<T> stores the values on the basis of FIFO. It keeps the order, in which the values were added.
It provides an Enqueue() method to add values and a Dequeue() method to retrieve values from the collection.
Stack<T>
Stack<T> stores the values as per the mechanism of Last In First Out.
It provides a Push() method to add a value and Pop() & Peek() methods to retrieve values.
ObservableCollection<T>
This is the special kind of collection data type, which have the unique feature of Receive notifications when items are removed or added to the collection (implements INotifyPropertyChanged and INotifyCollectionChanged)
LinkedList<T>
This allows to access items sequentially
Hashset<T>
Hashset<T> is contained unique values and has a mechanism to eliminate duplicate values
SortedSet<T>
It is a set for mathematical functions
Non-Generic Collection Data Type
These the old fashion collection data types which are now a day not recommended because it has less advantage compared to generic collections
Below is the list of non-generic collection
- ArrayList and Array
- SortedList
- Stack
- Queue
- HashTable
- BitArray
ArrayList and Array
ArrayList and Array stores objects of any type like an array. it is dynamic control and no need to specify the size of the ArrayList like with an array.
HashTable
HashTable is a collection of key/value pairs that are organized based on the hash code of the key.
SortedList
It works the same as HashTable works it stores values in the form of key and value pairs. It has a mechanism of arranging values in ascending order of key by default. It is in both, generic and non-generic SortedList collections.
Stack
It stores the value in the manner of Last In First Out. It has a Push() method to add a value and Pop() & Peek() methods to retrieve values. It is in both, generic and non-generic Stack.
Queue
Queue stores the values in the manner of First In First Out. It maintains the order in which the values were stored. It has an Enqueue() method to add values and a Dequeue() method to retrieve values from the collection. it is in both generic and non-generic Queue.
BitArray
BitArray is a special kind of array that stores bit values, which are used Boolean as data type, where true indicates that the bit is on (1) and false indicates the bit is off (0).
Features of Collections
- All Collection data types provide full control to add, remove and find value inside it.
- It gives access to copy the content of the collection to an array: by default, It provides the method of CopyTo(), and it will store the element into the new array based on the sequence.
- The ability to enumerate the collection: the collection has the ability to retrieve data by using a loop or we can grab it by using LINQ Expression.
- It has Capacity and Count properties: The capacity of a collection we can calculate on the basis of elements it can contain. The count of a collection is the actual number it contains. Some
Conclusion
Now, it is clear the Generic collection is much better compared to non-generic also it is recommended and a good practice to use generic.
Hope this will help, Kindly Share the feedback so that, I can improve my blogs
Thank you :)
Comments
Post a Comment