---
title: "How to create a custom generic class in C#? Provide an example."  
description: "How to create a custom generic class in C#? Provide an example."  
author: "Revati S Misra"  
published: 2023-11-03  
updated: 2023-11-05  
canonical: https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example  
category: "c#"  
tags: ["c#", "generics", "generic class"]  
reading_time: 2 minutes  

---

# How to create a custom generic class in C#? Provide an example.

How to create a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [generic class](https://www.mindstick.com/forum/158930/explain-the-concept-of-a-generic-class-and-how-it-enables-code-reusability-and-type-safety) in C#? Provide an example.

## Replies

### Reply by Aryan Kumar

Creating a custom generic [class in C#](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) is a powerful way to design reusable and type-safe components. Here's a step-by-step guide along with an example:

Step 1: Define a generic class by specifying a type parameter.

```plaintext
public class CustomGenericClass<T>
{
    private T data;

    public CustomGenericClass(T value)
    {
        data = value;
    }

    public T GetData()
    {
        return data;
    }

    public void SetData(T value)
    {
        data = value;
    }
}
```

In the example above, we define the **CustomGenericClass** with a type parameter **T**. This class can work with any data type specified when creating instances.

Step 2: Use the custom generic class.

```plaintext
class Program
{
    static void Main()
    {
        // Create an instance of the generic class with int data type.
        Custom
```

In this code, we create instances of **CustomGenericClass** with both **int** and **string** data types. The generic class can be used with different data types, and it provides methods to get and set the data of the specified type.

This example demonstrates how to create a custom generic class in C# and how to use it with different data types. It allows you to write flexible and type-safe code that can be reused for various scenarios.


---

Original Source: https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
