Skip to main content

Non-Generic Collection and Generic Collection in.NET

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

Popular posts from this blog

Interview Questions of SPFx SharePoint

What is SPFx? The SharePoint Framework (SPFx) is a web part model that provides full support for client-side SharePoint development, it is easy to integrate with SharePoint data, and extend Microsoft Teams. With the SharePoint Framework, you can use modern web technologies and tools in your preferred development environment to build productive experiences and apps that are responsive and mobile-ready. 1.  How to add SPFx web part to full-width column Answer:  You need to set  "supportsFullBleed": true, on the component.menifest.JSON 2. What is a gulp?  the term Gulp is an open-source JavaScript toolkit created by Eric Schoffstall used as a front-end web development streaming build system. Work with Node.js and npm, used for automation of time-consuming and repetitive tasks. Uses in web development like minification, concatenation, cache busting, unit testing, linting, optimization, etc 3. What is package.json in the SPFx? the term package.json file is an application’s directo

Interview Question of Advanced SharePoint SPFx

1. What is WebPack? A module bundling system built on top of Node. js framework is known as a WebPack. It is capable to handle the combination and minification of JavaScript and CSS files, also other files such as images by using plugins. WebPack is the recommended way of bundling the files in JS framework and .Net frameworks. in the SPFx it is used with React Js. 2. What is PowerShell.? PowerShell is a platform introduced by Microsoft to perform cross-platform task automation and configuration management framework, it is made up of a command-line shell, a scripting language. it can be run on Windows, Linux, and macOS. 3. What is bundling and Minification? The terms bundling and minification are the processes of code compressing, which is used to improve the load and request time, It is used in modern JavaScript frameworks and .Net frameworks. It improves the load time by reducing the number of requests to the server and reducing the size of requested such as CSS and JavaScript. it dec

Top20 - SharePoint Framework (SPFx) Interview Questions

1.  Which tool we can use to generate a SharePoint Framework (SPFx) solution? Developer can use  Yeoman to generate SharePoint Framework (SPFx) solution, it is a client-side scaffolding open-source tool to help in web-based development. 2. How developer can ensure that the solution deployed was immediately available to all site collections in SPFx?  To ensure the availability of the deployed solution on all the site collections developer has to configure  skipFeatureDeployment: true  in package-solution.json {     "solution" : {       "name" : "theultimateresources-deployment-client-side-solution" ,       "id" : "as3feca4-4j89-47f1-a0e2-78de8890e5hy" ,       "version" : "2.0.0.0" ,       "skipFeatureDeployment" : true     },     "paths" : {       "zippedPackage" : "solution/theultimateresources-deploy-true.sppkg"     }  }