From the course: .NET Fundamentals: Concepts, APIs, and Libraries in the .NET Framework

Unlock the full course today

Join today to access over 24,100 courses taught by industry experts.

Generics

Generics

- [Instructor] .NET and its languages are built on the concept of a strong type system, which means every variable and constant has a type, as does every expression that evaluates to a value. This is a benefit as strong typing helps prevent type mismatches and reduces the risk of type related errors. However, it can lead to code explosion where we have to create lots of specialized versions of our code. .NET generics provides a way to create reusable, type-safe code that can handle a range of data types without having to write the same code again and again. First, let's take a look at an example how code will work without generics. So on the top, I'm creating a simple implementation of a stack class. I've called it ObjectStacker. And on the bottom, I am using the ObjectStacker. So if you take a look at the code on the top, on line 57, I'm defining a Push method, and it takes an object as a parameter. Line 61 has the…

Contents