---
title: "What is the Generic and non generic collections?"  
description: "What is the Generic and non generic collections?"  
author: "Shrikant Mishra"  
published: 2019-10-05  
updated: 2023-06-07  
canonical: https://www.mindstick.com/interview/33647/what-is-the-generic-and-non-generic-collections  
category: "c#"  
tags: ["c#", ".net", "asp.net", "oops", "programming language"]  
reading_time: 3 minutes  

---

# What is the Generic and non generic collections?

These are two types of collections one is generic collection another one is non-generic collection.

## Generic Collection:

Into this, We can group one type of object in a single collection is called generic collection. Here we don't need to convert a particular type like string, int, etc.

## Non Generic Collection:

Into this, We can group any type of object in a single collection is called non-generic collection. Here we need to convert a particular type like string, int,etc., otherwise, we will face issues in run time.

## Answers

### Answer by Aryan Kumar

**Generic collections** are collections that can hold elements of any type. This is in contrast to non-generic collections, which can only hold elements of a specific type.

Generic collections are introduced in JavaScript ES2015. They are implemented using the class keyword.

Here is an example of a generic collection:

Code snippet

```plaintext
class MyCollection<T> {
  constructor() {
    this.items = [];
  }

  add(item: T) {
    this.items.push(item);
  }

  get items() {
    return this.items;
  }
}
```

This class can be used to create a collection that can hold elements of any type. For example, you could create a collection of strings or a collection of numbers.

**Non-generic collections** are collections that can only hold elements of a specific type. This is the older way of creating collections in JavaScript.

Here is an example of a non-generic collection:

Code snippet

```plaintext
var myArray = [];

myArray.push("Hello");
myArray.push(123);
```

This code creates an array that can hold elements of any type. However, it is not type-safe. This means that you could accidentally add an element of the wrong type to the array.

**Generic collections** are preferred over non-generic collections because they are type-safe. This means that you can be sure that the elements in a generic collection are of the correct type. This can help to prevent errors and improve the performance of your code.

Here is a table of the differences between generic and non-generic collections:

| Feature | Generic collections | Non-generic collections |
| --- | --- | --- |
| Type-safety | Yes | No |
| Performance | Better | Worse |
| Flexibility | More flexible | Less flexible |

It is important to note that generic collections are not supported by all browsers. If you need to support older browsers, you may need to use non-generic collections.

### Answer by Shrikant Mishra

These are two types of collections one is generic collection another one is non-generic collection.

## Generic Collection:

Into this, We can group one type of object in a single collection is called generic collection. Here we don't need to convert a particular type like string, int, etc.

## Non Generic Collection:

Into this, We can group any type of object in a single collection is called non-generic collection. Here we need to convert a particular type like string, int,etc., otherwise, we will face issues in run time.


---

Original Source: https://www.mindstick.com/interview/33647/what-is-the-generic-and-non-generic-collections

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
