What are generics?

In C#, generics are used to create classes, methods, structs and other components that are not specific, but general. This allows us to use the generic component for different reasons. For example, if you have a general-purpose soap, you can use that soap for any kind of washing. You can use it to wash your hands, to wash your clothes, or even to wash your dirty dishes. However, if you have a specific category of soap, such as laundry detergent, it can only be used for washing clothes and not for any other thing. Consequently, generics give us some extra power of re-usability in our code, which is good for an application as there would be less code which does similar work. Generics are not newly developed; they has been ...

Get Hands-On Object-Oriented Programming with C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.