Key Difference – Generic vsNon-GenericCollection 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 forstacks, queues,listsandhashtables. Thekey differencebetween Generic and Non-generic Collection in C# is thata 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.年代imilarities Between Generic and Non-Generic Collection in C#
5.年代ide by Side Comparison – Generic vs Non-Generic Collection in C# in Tabular Form
6.年代ummary
What is Generic Collection in C#?
The Non-generic collections such asArrayList, 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
The GenericList
List
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
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.
年代ortedList
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#?
Arrayscan 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.
年代ome 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 theforeach loop.
ArrayListis 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#.
什么是迪fference 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. |
年代toring 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. |
年代ummary –Generic vsNon-GenericCollection 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.”,教程点, 19 Mar. 2018.Available here
Image Courtesy:
1.’Logo C Sharp’By Microsoft, (Public Domain) viaCommons Wikimedia
Leave a Reply