Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / 编程 /Difference Between Generic and Non-Generic Collection in C#

Difference Between Generic and Non-Generic Collection in C#

April 5, 2018Posted byLithmee

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, Dictionary , SortedList , HashSet, Queue, Stack .

Difference Between Generic and Non-Generic Collection in C#

The GenericList 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 list1 – new 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 dictionary1 = new 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 s0 = new SortedList ();

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

Related posts:

Difference Between Enumeration and Iterator Difference Between Abstract Class and Concrete Class Difference Between Algorithm and FlowchartDifference Between Algorithm and Flowchart Difference Between Value Type and Reference TypeDifference Between Value Type and Reference Type Difference Between Attribute and ParameterDifference Between Attribute and Parameter

Filed Under:编程Tagged With:Elements of Generic Collection in C#,Elements of Non-Generic Collection in C#,Generic and Non-Generic Collection Differences,Generic and Non-Generic Collection Similarities,Generic Collection in C#,用c#泛型集合的定义,Generic Collection in C# Type,Namespace of Generic Collection in C#,Namespace of Non-Generic Collection in C#,Non-Generic Collection in C#,Non-Generic Collection in C# Definition,Non-Generic Collection in C# Type

About the Author:Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between CuSO4 (s) and CuSO4 (aq)

Difference Between Bolsheviks and Mensheviks

Difference Between Bolsheviks and Mensheviks

Difference Between Widgets and Apps

Difference Between E1 and E2 Reactions

Difference Between E1 and E2 Reactions

Difference Between Black and White Skin

Difference Between Black and White Skin

Latest Posts

  • 什么是迪fference Between Kaolinite and Illite
  • 什么是迪fference Between Axon Hillock and Initial Segment
  • 什么是迪fference Between Glycolic Acid and Hyaluronic Acid and Retinol
  • 什么是迪fference Between Strawberry Legs and Keratosis Pilaris
  • 什么是迪fference Between Type 1 and Type 2 Alveolar Cells
  • 什么是迪fference Between Hydrodissection and Hydrodelineation
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.