Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies.
I love to learn new things in life that keep me motivated.
To define a generic method in C#, you use the method-name<T> syntax, where
<T> represents the type parameter, and you can use it as a placeholder for a specific data type. Here are the steps to define a generic method:
Begin with the method declaration and specify the type parameter within angle brackets (<>) immediately after the method name.
Use the type parameter T within the method body to work with the generic type.
Optionally, you can add constraints to the type parameter to limit the allowable data types.
Here is a basic example of how to define a generic method:
public class GenericMethods
{
public T GenericMethod<T>(T value)
{
// Method body can work with the type parameter 'T'.
return value;
}
}
In this example, GenericMethod is a generic method that can work with any data type specified when calling the method. You can use it to return a value of the same type as the input argument.
You can also add constraints to the type parameter to ensure that it adheres to specific requirements. For example, you can constrain
T to implement the IComparable<T> interface, allowing you to use comparison methods within the generic method:
public class GenericMethods
{
public T FindMax<T>(T[] array) where T : IComparable<T>
{
if (array.Length == 0)
throw new ArgumentException("Array is empty");
T max = array[0];
foreach (T item in array)
{
if (item.CompareTo(max) > 0)
{
max = item;
}
}
return max;
}
}
In this case, the FindMax method is a generic method that can find the maximum value in an array of any data type
T as long as that type implements the IComparable<T> interface.
To use a generic method, you provide the type argument when calling the method. For example:
GenericMethods methods = new GenericMethods();
int[] intArray = { 42, 15, 28, 8 };
int maxInt = methods.FindMax(intArray); // Calls the generic method with int as the type argument.
string[] stringArray = { "apple", "banana", "cherry" };
string maxString = methods.FindMax(stringArray); // Calls the generic method with string as the type argument.
In the examples above, the generic method is called with different data types, and it adapts to work with each type while ensuring type safety and code reusability.
Liked By
Write Answer
How to define a generic method in C#?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
05-Nov-2023To define a generic method in C#, you use the method-name<T> syntax, where <T> represents the type parameter, and you can use it as a placeholder for a specific data type. Here are the steps to define a generic method:
Begin with the method declaration and specify the type parameter within angle brackets (<>) immediately after the method name.
Use the type parameter T within the method body to work with the generic type.
Optionally, you can add constraints to the type parameter to limit the allowable data types.
Here is a basic example of how to define a generic method:
In this example, GenericMethod is a generic method that can work with any data type specified when calling the method. You can use it to return a value of the same type as the input argument.
You can also add constraints to the type parameter to ensure that it adheres to specific requirements. For example, you can constrain T to implement the IComparable<T> interface, allowing you to use comparison methods within the generic method:
In this case, the FindMax method is a generic method that can find the maximum value in an array of any data type T as long as that type implements the IComparable<T> interface.
To use a generic method, you provide the type argument when calling the method. For example:
In the examples above, the generic method is called with different data types, and it adapts to work with each type while ensuring type safety and code reusability.