---
title: "How to create a generic class and method in C#?"  
description: "How to create a generic class and method in C#?"  
author: "Ethan Karla"  
published: 2021-08-27  
canonical: https://answers.mindstick.com/qa/93936/how-to-create-a-generic-class-and-method-in-c-sharp  
category: "programming"  
tags: ["c#", "java"]  
reading_time: 3 minutes  

---

# How to create a generic class and method in C#?

How to create a [generic class](https://www.mindstick.com/articles/327/collection-and-generic-collection-classes-in-c-sharp-dot-net) and [method in C#](https://www.mindstick.com/forum/33924/can-this-be-used-within-a-static-method-in-c-sharp)?

## Answers

### Answer by Ravi Vishwakarma

We can create a **generic [class](https://www.mindstick.com/articles/12089/arrays-in-java-determining-class-of-an-array-part-8)** in C# that can be used with objects of any type. This type of class is called Generic Class. We can also create a generic [method](https://www.mindstick.com/articles/23411/the-most-effective-method-to-find-the-perfect-small-business-phone-system-for-your-business) like a generic class which can be called with any type of argument. The compiler handles each method based on the arguments passed to the generic method. **Type Parameters** The naming convention of type parameters is very important to learn generics. 1. T – Type 2. E - Element 3. K – Key 4. N – Number 5. V – Value **Syntax**

```
Class class-name<T>
 {
      access-modifier datatype method-name(){
      }
      access-modifier T method-name(){
      }
 }
```

**Example**

```
using System;
//Generic Class
class Genericdemo <T>
{
 //Generic property
 private T[] _data = new T[10];
    public void AddOrUpdate(int index, T item)
    {
        if(index >= 0 && index < 10)
            _data[index] = item;
    }
  //Generic Method
    public T GetData(int index)
    {
        if(index >= 0 && index < 10)
            return _data[index];
    return default(T);
    }
}
public class Program
{
 public static void Main()
 {
  Genericdemo<string> gd= new Genericdemo<string>();  //Generic class object
  gd.AddOrUpdate(0,'Ashu');
  gd.AddOrUpdate(1,'Anupam');
  gd.AddOrUpdate(2,'Manish');
  gd.AddOrUpdate(3,'Sunny');
  gd.AddOrUpdate(4,'Shyam');
  for(int i=0;i<7;i++)
   Console.WriteLine(gd.GetData(i));  //Generic Method calling
 }
}
```

**Output**

```
Ashu
Anupam
Manish
Sunny
Shyam
```

\

### Answer by Ashish Roe

You can create an instance of generic classes by specifying an actual type in angle brackets. The following creates an instance of the generic class `DataStore`.

```plaintext
DataStore<string> store = new DataStore<string>();
```

Above, we specified the `string` type in the angle brackets while creating an instance. So, `T` will be replaced with a `string` type wherever `T` is used in the entire class at compile-time. Therefore, the type of `Data` property would be a `string`.

You can assign a string value to the `Data` property. Trying to assign values other than string will result in a compile-time error.

```plaintext
DataStore<string> store = new DataStore<string>();
store.Data = 'Hello World!';
//store.Data = 123; //compile-time error
```

You can specify the different data types for different objects, as shown below.

```plaintext
DataStore<string> strStore = new DataStore<string>();
strStore.Data = 'Hello World!';
//strStore.Data = 123; // compile-time error

DataStore<int> intStore = new DataStore<int>();
intStore.Data = 100;
//intStore.Data = 'Hello World!'; // compile-time error

KeyValuePair<int, string> kvp1 = new KeyValuePair<int, string>();
kvp1.Key = 100;
kvp1.Value = 'Hundred';

KeyValuePair<string, string> kvp2 = new KeyValuePair<string, string>();
kvp2.Key = 'IT';
kvp2.Value = 'Information Technology';
```


---

Original Source: https://answers.mindstick.com/qa/93936/how-to-create-a-generic-class-and-method-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
