In this tutorial, we will learn how to create Custom Generic Methods and Classes in C#. Generics are a powerful feature in C# that allow you to write a single class or method that works with a variety of types, without the need for casting or type checking.
By the end of this tutorial, you will understand:
- The concept of Generics in C#
- How to create Generic Classes
- How to create Generic Methods
To follow along with this tutorial, you should have:
- A basic understanding of C# and Object-Oriented Programming
- A development environment set up with C# (like Visual Studio or .NET Core)
Generics are a feature of C# and .NET that allow you to define type-safe data structures, without committing to actual data types. This results in a high level of code reusability and cleaner code.
A generic class can be defined using a type parameter in angle brackets after the class name. For example, the List<T>
class in .NET is a generic class.
public class MyGenericClass<T>
{
private T genericMemberVariable;
public MyGenericClass(T value)
{
genericMemberVariable = value;
}
public T genericMethod(T genericParameter)
{
Console.WriteLine($"Parameter type: {typeof(T)}, value: {genericParameter}");
Console.WriteLine($"Return type: {typeof(T)}, value: {genericMemberVariable}");
return genericMemberVariable;
}
public T genericProperty { get; set; }
}
Like generic classes, we can also have generic methods. A generic method is a method that is declared with type parameters, as shown below:
public void MyGenericMethod<T>(T parameter)
{
Console.WriteLine($"Parameter type: {typeof(T)}, value: {parameter}");
}
Below is an example of a generic class used to store a value of any type:
public class GenericClass<T>
{
private T genericValue;
public GenericClass(T value)
{
this.genericValue = value;
}
public T GetValue()
{
return this.genericValue;
}
}
In this example, T
is a placeholder for any type. You can create an object of GenericClass
with any type:
var intClass = new GenericClass<int>(10);
Console.WriteLine(intClass.GetValue()); // Output: 10
var stringClass = new GenericClass<string>("Hello World");
Console.WriteLine(stringClass.GetValue()); // Output: Hello World
Here is an example of a generic method that can accept parameters of any type:
public void DisplayValue<T>(T value)
{
Console.WriteLine($"The value is: {value}");
}
This method can be called with any type of parameter:
DisplayValue<int>(100); // Output: The value is: 100
DisplayValue<string>("Hello World"); // Output: The value is: Hello World
In this tutorial, we have learned:
- What are Generics in C# and why to use them
- How to create Generic Classes and Methods
To further your understanding of Generics, you can:
- Learn about constraints in Generics
- Understand the difference between Covariance and Contravariance in Generics
Pair
that takes two parameters of any type and has a method to display both.Pair
:public class Pair<T1, T2>
{
private T1 firstValue;
private T2 secondValue;
public Pair(T1 first, T2 second)
{
this.firstValue = first;
this.secondValue = second;
}
public void Display()
{
Console.WriteLine($"First: {firstValue}, Second: {secondValue}");
}
}
public bool ContainsValue<T>(T[] array, T value)
{
return array.Contains(value);
}