From the course: C# Essential Training 2: Generics, Collections, and LINQ

Working with generics

- [Instructor] When you're working with C sharp and generic types, you're going to either be working with an actual generic type, meaning a class, a record that has type parameters, or a generic method. So a generic method might be something like we've seen. I'm just going to piece in a little bit of code here. So this is just a string that represents some JSON, some JavaScript object notation. Pretty common format these days for serializing objects and it just indicates that we've got an ID property, first name, last name, and age, which should look familiar. That's what our person class looks like. So now we might do something like VAR PJ, or person from JSON equal system.text.json and we want to use what's called the JSON serializer that's going to allow us to serialize and deserialize objects and you'll notice the first option is a deserialized method that returns an object, but I know that the JSON I have represents a person, be deserialized into a person, it's going to choose the generic method and I can see here then that it has a type parameter, it just says T value and I can also see that the return type is a knowable T value. So in here, I could say I want this to be person and then I'm going to pass it the JSON person text, and I should now have a person object that's been deserialized from that text. We could do something like this. We write out to the console, say JSON person, and then we can do PJ and I'll use this because it is a nullable person, so that helps me make sure that I avoid any null references. I'll do first name, then I can come across and say is, and then we could do PJ question mark.age Got an extra semi colon there. So that's a generic method, where you're going to give the type of the method and that's generally going to map onto one or more parameters, or the return type or both. We'll run this quick and we should see that JSON person come out as our output at the beginning and there we see JSON person, Matt is 50 and if we look again, that's what we had in our text. Our JSON, first name was Matt, age was 50. In C Sharp Essentials Part One, we actually worked with a generic type, albeit a little bit indirectly. We worked with nullable value types like this. What that shortcut was really referencing was a nullable of T int. So we can write it like this. That's a generic type. What does that mean? Well, if we look at X now, we can see that there are properties on there called value and it has a return type of int 'cause that's the generic type parameter we provided. If we look at git value or default, that's going to return an int. Again, because of the type parameter that we provided. It may be that what it's going to provide is zero because that's the default for an int, or it's going to provide us with that value. Likewise, we take this now and we do a nullable of date time. We'll call it maybe date, and we'll be explicit. We'll set it to null for now. Now if we look at maybe date, the value again is date time. So it's still a nullable, but it's got a type parameter that is going to be used throughout that class, the nullable class, in order to represent return values of parameters of properties and methods. So our value has the date time. Again, if we went to the git value or default, the return type is going to be a date time, but it may be a valid date time, or in our case, it's going to be the default. So let's go ahead and do git value or default. We can write that out and we'll see if we run this, what do we get? We got a build error there. Oh, didn't like my X. We'll call that A, 'cause I already have X down below. There's a JSON person, and there's our default value of the date. We said git value or default. Default is January 1st, 0001 at 12:00 AM. So when you're using generic types, you're going to provide one or more of these generic parameters and that's going to influence parameter types, return types in that class or for that method, depending on the scope at which you're using the generic declaration.

Contents