Generic Class
Let's learn about generic classes.
We'll cover the following
What are generics?
Generics define type-safe classes without committing to any specific data types. They’re essentially a placeholder until a specified data type is declared. There are many prebuilt classes that use generics. This lesson covers how to create these generic classes.
The syntax below demonstrates the use of the generic type parameter, which uses the angle brackets (<>
). Generic type names are usually a single capital letter, T
being the most common, or a simple name starting with T
, such as TKey
or TValue
.
Syntax
class AGenericClass<T>
{
// Empty generic class
}
Example: Generic class
Assume that the generic class in the next example is being instantiated with a string in the following way:
Get hands-on with 1400+ tech skills courses.