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

Power Apps modern driven app Interview Question and answer

Question 1: What is a Power Apps modern driven app? Answer: A Power Apps modern driven app is a low-code/no-code application development platform provided by Microsoft. It allows users to create custom applications that can run on various devices and platforms without the need for extensive coding. These apps can be built using a visual interface and can integrate with different data sources. Question 2: What are the key components of a Power Apps modern driven app? Answer: The key components of a Power Apps modern driven app are: Screens : Screens serve as the user interface for the app and can include multiple layouts, controls, and data visualizations. Data sources : These are the various repositories where the app can retrieve and store data, such as SharePoint lists, SQL databases, Excel files, etc. Connectors : Connectors enable integration with external services and systems, allowing data to be fetched or updated. Formulas : Power Apps uses a formula language called Power Apps

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. Scenario Based asking Scenario 1: Scenario: Your team is developing a SharePoint Framework (SPFx) web part that needs to retrieve data from an external API and display it on a SharePoint site. The API requires authentication using OAuth 2.0. The web part should also allow users to refresh the data manually. Question 1: How would you approach implementing this functionality in an SPFx web

SPFX Interview question for 2023

SPFx Interview Questions for 2023 Question 1: What is SharePoint Framework (SPFx)? Answer: SharePoint Framework (SPFx) is a development model introduced by Microsoft for creating client-side web parts and extensions for SharePoint Online and SharePoint 2019. It is based on modern web technologies like JavaScript, TypeScript, and React, providing a rich and responsive user experience. Question 2: What are the key advantages of using SPFx for SharePoint development? Answer: SPFx offers several advantages, such as: Responsive and mobile-ready web parts and extensions. Seamless integration with SharePoint data and services. Support for modern web development practices and tools. Easy deployment and hosting options. Enhanced security and isolation through the SharePoint app model. Question 3: Can you explain the structure of an SPFx solution? Answer: An SPFx solution consists of the following key components: Web Parts: These are the building blocks of SPFx solutions, representing the vi