Covariance and Contravariance Clearly Explained in C# - A Deeper Look

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up amigos welcome to another video of a deeper load in this video you are going to overtake the concept of variance in c-sharp strictly speaking we will talk about covariance and contravariance including a look at how they are applied to generic type parameters so without further to do let's dig in I have created a new console application with dotnet core 3.1 and c-sharp eight you will be fine with any version of.net newer than version four because this is the version which has started this whole thing okay in this video I need you to know about two types in dotnet both of them are available in system namespace first one is the action delegate this delegate can hold reference to methods that take one parameter or up to 16 parameters as input no return type is specified so a return type is void another one is function delegate this one is similar to the previous one with one major difference this one also takes an output type parameter so we can specify output type now let's start with creating three types that we need for this video first one is the class of vehicle the second one is a class of car and this one drives from Vico third one is the class of aircraft this one also drives from vehicle now let's talk about covariance now let's create two cars and assign them to Vito now let's create an aircraft and assign it to a visual as you can see all the cars are vehicles but not all the witches are cars this one is an aircraft and the other ones are cars now let's create a method that returns a vigil let's assign this method to a function then we call this function we receive a vehicle now what if we change the Vita to a car we are calling this function that returns us a vehicle but the underlying metal is always returning a car so you are looking at the method that returns a car from the eyes of a method that always returns a meter this is called covariance because the car is covariant with Vito a car is a more drive class and vehicle is a less drived class so a car is implicitly casted to a veto now let's create another method this method returns void and accepts a veto let's assign this new method to an action delegate that receives a little now we can pass the vigil to this action this is a normal thing that we do every day but let's change it a bit we change this week to a car this is now a little bit counterintuitive you are assigning a method that accepts a feature to an action that I accepts a car but how is this working if we look at this line from the perspective of this action we have a method that accepts a car but from this side we are accepted which are if we assign a car to the DS side a car will be implicitly tested to a veto so we have no problem so let's change this vehicle to a car this is called contravariant because we are assigning a less draft class to a more draft class now before we go any further and extend our knowledge to the area of the type parameters of generics I would like to ask you subscribe to my channel and an angle the notification bear this will help me a lot and you will be notified whenever a new video is available thanks for your support now let's talk about covariant and contravariant in generic type parameters let's create an interface we call this I call variance in this interface we specify a type parameter of T and then we will add a property that only supports a gator this property is of type tea we also create a method that returns tea now let's create a new reference of type I covariant the type parameter we specify here is going to be a video on the right hand side because we don't have any class that implements a covariant we just specify a null and we cast this null to i covariant of nature so this is just the trick we could have a class here that implements a covariant and being Senshi ated here now if you remember from our example here we were assigning a more specific and a more Drive type to a less drive time now let's do this with these interfaces a more drive time to a less drive time and in our example if you had just getters like this one but we are getting errors here why is that but c-sharp is not going to understand this if we not explicitly explained it to it so if we want to explain this to c-sharp you have to specify a keyword here since we are outputting objects from this interface you specify an out here now you can see the red lines disappear so we are assigning a more Drive type to a less Drive type now let's create another interface we call this 1i contra variant this has also app type parameter of T in this interface we have a property that has only setters and a method that returns void and accepts a parameter of type T now let's create another reference this time from a contravariant and we specify feature as it's time we do the same trick here because we are not going to create another class now if you remember in contravariant we assigned a less Drive type to a more Drive type so let's do this we change this feature to a car and we are getting errors now we have to explicitly tell seashore this is a contravariant generate we do this with a specifying another keyword called in since this interface only accepts inputs and now this one compiles so we are assigning a less Drive type to a more Drive type that's it now let's see something interesting if we look at system that action all the type parameters are specified as in so action type parameters are all contravariant now what about function if we see function we will see the return type is specified as out and all the inputs are specified as in so the return type is covariant and the input types are contravariant now let's take a look at a real world example for contravariant I have created an interface of I compare this is a contravariant fun I have a specified weekend as its type parameter we are comparing which as based on the air speed I have also added a property of speed to retails now let's create a sorted set of cars be passing or VCAL compare and add a few cars to this now we print the speed so that we know this is working now let's run the application as you can see this has sorted all the crops please life is medium and leave a comment if you have any question ask me in the comments and tell me what you would like to see next thanks for watching and until next time adios angles
Info
Channel: Full Stack Amigo
Views: 7,409
Rating: undefined out of 5
Keywords: programming, c#, .net, code, computer science, fullstack, full stack amigo, covariance, contravariance, covariant, contravariant, coding, visual studio, generics, generic types, c# coding, c# covariant, c# out, c# in, c# keyword, contravaiant in c#, سی شارپ, دات نت, جنریک, جنریک در سی شارپ, c# generics, software development, software engineer, tutorial, c sharp, full stack, full stack developer, C# out keyword, C# in keyword, clearly explained, covariant in c#, advanced c#
Id: LsKIhuRJbtk
Channel Id: undefined
Length: 14min 18sec (858 seconds)
Published: Fri Feb 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.