What is generic in C#?

What is generic?

“Generic” is derived from the simple word “general”. In C#, generic allows us to generalize a method or a class for any data type of programming element. In simpler words, generic allows us to write any class or method that can be used for any data type that is included in the definition.

We can create a generic class with generic fields and methods.

Generic fields and methods

Generic fields and methods are types or functions in a programming language that allows for the use of one or more types as parameters, rather than being fixed to a specific type. This allows for greater flexibility and reusability of code, as the same fields or methods can be used with multiple types without having to create separate versions for each type. Generic types are often denoted using angle brackets < > with the type parameter(s) listed within.

Advantages of generic

We use generic in C# due to certain advantages it provides such as:

  • Generic can help us reuse the code in certain instances without worrying about the data type that it can handle.
  • Generic are type-safe. That means compile-time errors are generated when we try to use data types that are not specified in the definition.
  • Generic removes the possibilities of boxing and unboxing and thus generates a performance advantage for the developer.

Example

The following code consists of a generic class that is used in the main program to create an array of integers and characters without generating a compile-time error related to data types.

public class GenericArray<T> { //creating a generic class
private T[] array;
public GenericArray(int size) { //creating a generic method
array = new T[size + 1];
}
public T getItem(int index) { //creating a generic method
return array[index];
}
public void setItem(int index, T value) { //creating a generic method
array[index] = value;
}
}
class Program {
static void Main(string[] args) {
//declaring an int array
GenericArray<int> intArray = new GenericArray<int>(5);
//storing integer in an integer array
for (int c = 0; c < 5; c++) {
intArray.setItem(c, c*5);
}
System.Console.Write("The values stored in an int array using a generic class are as follows: ");
//printing the values of an int array
for (int c = 0; c < 5; c++) {
System.Console.Write(intArray.getItem(c) + " ");
}
System.Console.WriteLine();
//declaring a character array
GenericArray<char> charArray = new GenericArray<char>(5);
//storing character in a character array
for (int c = 0; c < 5; c++) {
charArray.setItem(c, (char)(c+97));
}
System.Console.Write( "The characters stored in an character array using a generic class are as follows: ");
//printing the characters stored in a character array
for (int c = 0; c< 5; c++) {
System.Console.Write(charArray.getItem(c) + " ");
}
}
}

Code explanation

  • Lines 1–13: This consists of a generic class public class GenericArray<T> that contains generic methods public GenericArray(int size) for generating an array (lines 4–6), adding an element to the array using public void setItem(int index, T value) (lines 10–12) and accessing the specific element of the array using public T getItem(int index) (Lines 7–9). It is to be noted that these methods are generic, meaning these are not bound to any specific data type, i.e., we can generate a character array and an integer array using the same class. This is demonstrated in the main part of the program, as discussed in further explanation.

  • Lines 15–51: This consists of the main class in which we derive the generic class GenericArray and use it to generate an integer array and character array.

  • Lines 18–35: This is the declaration of an integer array intArray and a character array charArray, respectively.

  • Lines 21–23 We store the integers in the integers array using a for loop, whereas in lines 38–40, we store characters in a character array.

  • Lines 28–30: This prints the values stored in an integer array, and in a specific manner, lines 44–46 prints the character in the character array using the generic method and a counter-controlled for a loop.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved