Back to course

Introduction to Generics

C# Zero to Hero: Comprehensive Programming Masterclass

Generics

Generics allow you to write a class or method that can work with any data type, without losing type safety.

Example

csharp class MyGenericClass<T> { public T data; }

MyGenericClass<int> obj = new MyGenericClass<int>(); obj.data = 10;

T is a placeholder for a type that will be specified later.