Key Difference – Generic vs Non-Generic Collection in C#
A Generic collection is a class that provides type safety without having to derive from a base collection type and implement type-specific members. A Non-generic collection is a specialized class for data storage and retrieval that provides support for stacks, queues, lists and hashtables. The key difference between Generic and Non-generic Collection in C# is that a Generic Collection is strongly typed while a Non-Generic Collection is not strongly typed.
CONTENTS
1. Overview and Key Difference
2. What is Generic Collection in C#
3. What is Non-Generic Collection in C#
4. Similarities Between Generic and Non-Generic Collection in C#
5. Side by Side Comparison – Generic vs Non-Generic Collection in C# in Tabular Form
6. Summary
What is Generic Collection in C#?
The Non-generic collections such as ArrayList, Queue, Stack, etc. can store elements of different data types. When obtaining the items, the programmer should type cast them to the correct data type. Else, it can cause a runtime exception. The generic collection classes can be used to overcome this issue. Generic collections store elements internally in arrays of their actual types. Therefore, type casting is not required. They can be used to store elements of the specified type or types. Some Generic collection classes are List<T>, Dictionary <TKey, TValue>, SortedList <TKey, TValue>, HashSet<T>, Queue<T>, Stack<T> .
The GenericList<T> contains elements of specified type. It can increase the list accordingly when adding elements. When there is a statement as follows, all the elements that can be stored to the list1 should be integers,
List<int> list1 – new List<int> ();
The Generic Dictionary in C# is a collection of keys and values. When there is a statement as follows, the object dictionary1 can store int type keys and string type values.
Dictionary <int, string> dictionary1 = new Dictionary <int, string> ();
A Generic SortedList collection stores key and value pairs in ascending order of key by default. Below example stores key of int type and value of string type.
SortedList<int, string> s0 = new SortedList<int, string> ();
Those are few examples for Generic Collection in C#. These collections can store multiple values of the specified data types. So, they are strongly typed.
What is Non-Generic Collection in C#?
Arrays can be used to store multiple elements. One drawback is that it can store elements of the same data type. There are classes in C# that can be used to store many values or objects known as collections. Collections help to store, update, delete, search, sort objects. The size of the collection can be increased or decreased dynamically.
Some Non-generic Collection classes are ArrayList, SortedList, Stack, Queue and HashTable. Each collection class implements the IEnumerable interface. It helps to iterate through the elements of the items in the collection using the foreach loop.
ArrayList is an alternative to an array. If there is an array that can store 10 elements, it cannot store 20 elements. If the array initialized to 10 elements but only stores 5 elements, then the rest is a not used. Therefore, an array is fixed. In an ArrayList, it is possible to add or remove elements depending on the index. It allows dynamic memory allocation. The sort method can be used to sort the elements in ascending order.
The HashTable is used to represent a collection of key value pairs. They are organized based on the hashCode of the key. Therefore, every element has a key value pair. The key can be used to access a particular element in the collection. The Stack represents the last in, first out access to items. The Queue is used for first in first out access of items. Those are some of the Non-generic Collections supported by C#. These collections can store elements of different types.
What is the Similarity Between Generic and Non- generic Collection in C#?
- Both Generic and Non-Generic Collection can be used to store multiple elements in C#.
What is the Difference Between Generic and Non-Generic Collection in C#?
Generic vs Non-Generic Collection in C# |
|
A Generic collection is a class that provides type safety without having to derive from a base collection type and implement type-specific members. | A Non-generic collection is a specialized class for data storage and retrieval that provides support for stacks, queues, lists and hash tables. |
Namespace | |
The Generic Collection classes are in the System. Collections. Generics namespace. | The Non -generic Collection classes are in the System. Collections namespace. |
Type | |
A Generic Collection is strongly typed. | A Non-Generic Collection is not strongly typed. |
Storing Elements | |
The Generic Collections store elements internally in arrays of their actual types. | The Non-generic collections store elements internally in object arrays so it can store any type of data. |
Summary – Generic vs Non-Generic Collection in C#
This article discussed the difference between Generic and Non-generic Collection in C#. The difference between Generic and Non-generic Collection is that a Generic Collection is strongly typed while a Non-generic Collection is not strongly typed.
Reference:
1.“When to Use Generic Collections.” Microsoft Docs. Available here
2.“Generic Collection in C#.” TutorialsTeacher.com. Available here
3.“C# Collection:” Collection in C#, TutorialsTeacher.com. Available here
4.“C# Collections.”, Tutorials Point, 19 Mar. 2018. Available here
Image Courtesy:
1.’Logo C Sharp’By Microsoft, (Public Domain) via Commons Wikimedia
Leave a Reply