Generic types
Both reference types and value types can be generic. We have already seen examples of generic types earlier in this book, such as Nullable<T>
and List<T>
.
In this section, we will learn how to create generic classes, structures, and interfaces.
Generic classes
The creation of generic classes is no different than non-generic classes. The only thing that differs is a list of type parameters and their use in the class as a placeholder for actual types. Let's look at an example of a generic class:
public class GenericDemo<T> { public T Value { get; private set; } public GenericDemo(T value) { Value = value; } public override string ToString() => $"{typeof(T)} : {Value}"; }
Here, we have defined a generic class, GenericDemo
, that is accepting one type parameter...