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 - .NET Tutorial
From the course: .NET Fundamentals: Concepts, APIs, and Libraries in the .NET Framework
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
-
-
-
-
(Locked)
The Common Type System (CTS)7m 41s
-
(Locked)
Namespaces and assemblies7m 5s
-
(Locked)
Interfaces5m 58s
-
(Locked)
Generics7m 22s
-
(Locked)
Delegates and Lambda expressions7m 51s
-
(Locked)
Reflection and attributes8m 32s
-
(Locked)
Nullable value types6m 39s
-
(Locked)
Arrays and collections6m 24s
-
(Locked)
Events and event handlers6m 42s
-
(Locked)
Handling exceptions in .NET8m 52s
-
(Locked)
Strings and Chars5m 50s
-
(Locked)
C# records and .NET3m 54s
-
(Locked)
Auto memory management with garbage collection9m 56s
-
(Locked)
Command line and the SDK5m 56s
-
(Locked)
Publishing applications2m 7s
-
(Locked)
-