“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 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.
We use generic in C# due to certain advantages it provides such as:
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 classprivate T[] array;public GenericArray(int size) { //creating a generic methodarray = new T[size + 1];}public T getItem(int index) { //creating a generic methodreturn array[index];}public void setItem(int index, T value) { //creating a generic methodarray[index] = value;}}class Program {static void Main(string[] args) {//declaring an int arrayGenericArray<int> intArray = new GenericArray<int>(5);//storing integer in an integer arrayfor (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 arrayfor (int c = 0; c < 5; c++) {System.Console.Write(intArray.getItem(c) + " ");}System.Console.WriteLine();//declaring a character arrayGenericArray<char> charArray = new GenericArray<char>(5);//storing character in a character arrayfor (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 arrayfor (int c = 0; c< 5; c++) {System.Console.Write(charArray.getItem(c) + " ");}}}
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