Generics In C#

In this article, We will learn about Generics in C#.

  • Generic means not specific to a particular data type.
  • C# allows us to create generic classes, interfaces, fields, methods, static methods, properties, events, and delegates by using the type parameter and without the specific data type.
  • A type parameter is a placeholder for a particular type.
  • A generic type is declared by specifying a type parameter in an angle bracket after a type name or class name.
  • Type parameter specified when creating an instance of the generic type or Class.

e.g.

TypeName<T>

 Where T is a Type Parameter. 

Generic Class

Generic classes are defined by using a type parameter in angle brackets after the class name. 

Syntax,

Class class_name<T>
{
  public T property_name {get; set;}
}

Example : 

Class Employee<T>
{
 public T EmployeeNo {get; set; }            
 public T SalaryCalculation()
 {
   //write code here…
 }
}

Above, the Employee is a generic class. T is called a type parameter, which can be used as a type of fields, properties, method parameters, return types, and delegates in the Employee class.

 For example,

  • EmployeeName is generic property because we have used a type parameter T as its type instead of the specific data type.
  • SalaryCalculation is a generic method because we have used a type parameter T as its return type instead of the specific data type.

Note:

  • It is not required to use T as a type parameter. You can give any name to a type parameter.
  • Generally, T is used when there is only one type parameter.
  • It is recommended to use a more readable type parameter name as per requirements like TSession, TKey, TValue, etc. Learn more about Type Parameter Naming Guidelines

You can also define multiple type parameters separated by a comma.

Example:

Class MultiType<Tkey, Tvalue>
{
 public Tkey No {get; set; }
 public Tvalue Name {get; set; }
}

Instantiating Generic Class 

 You can create an instance of generic classes by specifying an actual type in angle brackets. 

For example,

The following creates an instance of the above created a generic class Employee. 

Employee<int> emp = new Employee<int>();
emp.EmployeeNo = 116;

Above, we specified the int type in the angle brackets while creating an instance. So, T will be replaced with an int type wherever T is used in the entire class at compile time. Therefore, the type of EmployeeNo property would be an int, and the return type of SalaryCalculation Method will be int. 

You can assign an int value to the EmployeeNo property. Trying to assign values other than int will result in a compile-time error. 

Employee<int> emp = new Employee<int>();
emp.EmployeeNo = 116;
//emp.EmployeeNo = “Hello World”; //compile-time error

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

Employee<string> emp = new Employee<string>();
emp.EmployeeNo = "116";
//emp.EmployeeNo = 116; // compile-time error 

Employee<int> emp = new Employee<int>();
emp.EmployeeNo = 116;
//emp.EmployeeNo = “Hello World”; //compile-time error 

MultiType<int, string> multi = new MultiType<int, string>();
multi.No= 100;
multi.Name= "Hundred"; 

MultiType< string, string> multi = new MultiType< string, string>();
multi.No= "Hundred";
multi.Name= "Hundred";

Generic Class Characteristics :

  • A generic class increases the reusability. The more type parameters mean more reusable it becomes. Because too much generalization makes code difficult to understand and maintain.
  • A generic class can be a base class for other generic or non-generic classes or abstract classes.
  • A generic class can be derived from other generic or non-generic interfaces, classes, or abstract classes.

Advantage

  • Generic has a performance advantage because it removes the possibilities of boxing and unboxing.
  • Generics are type-safe. You get compile-time errors if you try to use a different data type than the one specified in the definition.
  • Generics increase the reusability of the code. You don’t need to write code to handle different data types.

I hope you guys, This will help you to understand the Concept of Generic in C#.

Thank you.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories