Hour 12. Understanding Generics
You have already seen how an object can refer to an instance of any class. This enables you to create classes that operate on any data type. Several significant problems can occur with this approach. By working only with object
, there is no way for the class to restrict input to be only of a specific type. To perform meaningful operations on the data, it must be cast from object
to a more well-defined type. This not only adds complexity, but also sacrifices type safety at compile time.
Generics in C# solve this problem ...
Get Sams Teach Yourself C# 5.0 in 24 Hours 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.