Skip to main content

Non-Generic Collection and Generic Collection in.NET

What is Collection in .NET?

What is Collection in .NET?

By Vishal Thakur

Introduction

Microsoft .NET provides a variety of collection data types that can store a set of records or values. There are two types of collections in .NET: generic collections and non-generic collections. Generic collections are type-safe at compile time, providing better performance compared to non-generic collections. They support type parameters when constructed and do not require boxing-unboxing or type conversion from the Object type when adding or removing items. Non-generic collections store items as Objects, require casting, and are not recommended for modern development. Most developers prefer generic collections because they are faster, safer, and less prone to exceptions and compile-time errors.

To use non-generic collections, you need to include the System.Collections namespace. For generic collections, use the System.Collections.Generic namespace.

Generic Collections Data Types

The term Generics refers to a general type, not a specific one. Generics maximize code reuse, type safety, and performance. The most common use of generics is to create collection classes. The namespace used for generics is System.Collections.Generic.

The letter T represents generics and acts as an encapsulation for data types.

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

Generics can be classes, structures, interfaces, or methods with parameters. Below is an example of a generic class:

public class GenericClass<T>
{
    public T Field;
}

To instantiate a generic class or method, you need to specify the actual type. Below is an example of how to use the generic class:

public static void Main()
{
    GenericClass<string> instanceObject = new GenericClass<string>();
    instanceObject.Field = "This is my string";
    Console.WriteLine("My Generic Field = \"{0}\"", instanceObject.Field);
    Console.WriteLine("Generic.Field.GetType() = {0}", instanceObject.Field.GetType().FullName);
}

List of Generic Collections

  • List<T>: Stores elements of a specified type and grows dynamically.
  • Dictionary<TKey, TValue>: Stores key-value pairs.
  • SortedList<TKey, TValue>: Stores key-value pairs in ascending order by key.
  • Queue<T>: Stores elements in a First-In-First-Out (FIFO) manner.
  • Stack<T>: Stores elements in a Last-In-First-Out (LIFO) manner.
  • LinkedList<T>: Allows sequential access to items.
  • ObservableCollection<T>: Notifies when items are added or removed.
  • HashSet<T>: Stores unique values and eliminates duplicates.
  • SortedSet<T>: A set for mathematical functions.

Non-Generic Collection Data Types

Non-generic collections are older and less recommended due to their disadvantages compared to generic collections. Below is a list of non-generic collections:

  • ArrayList: Stores objects of any type dynamically.
  • SortedList: Stores key-value pairs in ascending order by key.
  • Stack: Stores elements in a LIFO manner.
  • Queue: Stores elements in a FIFO manner.
  • HashTable: Stores key-value pairs organized by hash code.
  • BitArray: Stores bit values (true/false).

Features of Collections

  • All collection data types provide full control to add, remove, and find values.
  • They allow copying content to an array using the CopyTo() method.
  • They support enumeration using loops or LINQ expressions.
  • They have Capacity and Count properties to manage elements.

Conclusion

Generic collections are more efficient and safer compared to non-generic collections. It is recommended to use generic collections in modern .NET development to avoid exceptions and improve performance.

I hope this guide helps you understand collections in .NET. Please share your feedback so that I can improve my blogs. Thank you! :)

Related Articles

Comments

Popular posts from this blog

Power Apps modern driven app Interview Question and answer

Power Apps Modern Driven App: Questions and Answers Power Apps Modern Driven App: Questions and Answers Power Apps modern driven apps are low-code/no-code platforms that enable users to create custom applications. Below are some common questions and detailed answers to help you understand their capabilities and applications. 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 extensive coding. These apps are 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: Serve as the user interface for the app, including layouts, ...

SPFX Interview question for 2023

SPFx Interview Questions for 2023 SPFx Interview Questions for 2023 Author: Vishal Thakur 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 ...

Interview Questions of SPFx SharePoint

SPFx Interview Questions and Scenarios SPFx Interview Questions and Scenarios By Vishal Thakur What is SPFx? The SharePoint Framework (SPFx) is a web part model that provides full support for client-side SharePoint development. It integrates seamlessly with SharePoint data and extends Microsoft Teams. SPFx allows developers to use modern web technologies and tools in their preferred development environment to build responsive and mobile-ready experiences. Scenario-Based Questions Scenario 1: External API Integration Scenario: Your team is developing an 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: How woul...