Тёмный

What is Covariance and Contravariance in C#: A Complete Overview 

Zoran Horvat
Подписаться 25 тыс.
Просмотров 20 тыс.
50% 1

Are you a programmer struggling to understand what are the covariance and contravariance in C#? Look no further! In this video, I'm going to cover everything you need to know about covariance and contravariance in C#. As an expert in C# programming, I know that understanding covariance and contravariance is essential for working with generics and functional programming. That's why we've created this comprehensive video to explain the differences between covariance and contravariance, and how they can be used in programming. My video covers topics like the object substitution principle, variance, contravariant, covariance in C#, and contravariance. I'll walk you through examples and provide a clear explanation of how these concepts work in practice.⚡️❤️
With My guidance, you'll be able to master C# programming and make the most of these powerful concepts. My channel provides a deep dive into C# programming, including C# tutorials, tips, and tricks. I cover everything from the basics of C# to advanced topics like functional programming. With Zoran Horvat, you'll have access to the best C# programming tutorials on RU-vid.❤️
So, if you're looking to understand covariance and contravariance in C# or want to learn more about C# programming in general, be sure to subscribe to our channel for the latest insights and tutorials.🔥❤️
✅🔔Become a patron and get access to source code and exclusive live streams: / what-is-and-in-c-81378570
✅🔔 Subscribe ► / @zoran-horvat
⭐ Learn more from video courses:
Beginning Object-oriented Programming with C# ► codinghelmet.com/go/beginning...
⭐ Collections and Generics in C# ► codinghelmet.com/go/collectio...
⭐ Making Your C# Code More Object-oriented ► codinghelmet.com/go/making-yo...
🗳 Pluralsight Courses ► codinghelmet.com/go/pluralsight
📸 Udemy Courses ► codinghelmet.com/go/udemy
▬▬▬▬▬▬▬▬▬▬▬
⚡️ Have a look at our other Videos :
👉 Other videos on this channel you may be interested in watching:
Using GitHub Copilot to Write Complex Code | Step-by-step Tutorial ► • Using GitHub Copilot t...
👉 Coding with GitHub Copilot - Beginner to Master | VS Code Demo ► • A Comprehensive Guide ...
👉 What is Covariance and Contravariance in C# ► • What is Covariance and...
How to Initialize a Clean ASP.NET Core Project with Entity Framework Core and Identity ► • How to Initialize a Cl...
⚡️Chapters:
▬▬▬▬▬▬▬▬▬▬▬
⌚ 0:00 Today's Topic: Explained covariance and contravariance in c#
⌚ 0:22 What is the fundamental principle of object-oriented programming?
⌚ 1:16 How Object Substitution Principle works on regular types
⌚ 3:36 What is Variance?
⌚ 4:46 What is contravariant?
⌚ 5:12 What is the consequence of having this producer as covariant and this consumer as contravariant interfaces?
⌚ 8:48 What would happen if we tried to assign these interface references one to another?
⌚ 11:30 Courses and Outro
▬▬▬▬▬▬▬▬▬▬▬
#covariance #contravariance #generics #variant #variance #csharp #dotnet #zoranhorvat
▬▬▬▬▬▬▬▬▬▬▬
⭐ CONNECT WITH ME 📱👨
🌐Buy me a Coffee ► ko-fi.com/zoranhorvat
🗳 Pluralsight Courses ► codinghelmet.com/go/pluralsight
📸 Udemy Courses ► codinghelmet.com/go/udemy
📸 Join me on Twitter ► / zoranh75
🌐 Read my Articles ► codinghelmet.com/articles
📸 Join me on LinkedIn ► / zoran-horvat
▬▬▬▬▬▬▬▬▬▬▬
👨 About Me 👨
Hi, I’m Zoran, I have more than 20 years of experience as a software developer, architect, team lead, and more. I have been programming in C# since its inception in the early 2000s. Since 2017 I have started publishing professional video courses at Pluralsight and Udemy and by this point, there are over 100 hours of the highest-quality videos you can watch on those platforms. On my RU-vid channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
▬▬▬▬▬▬▬▬▬▬▬
⚡️RIGHT NOTICE:
The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorised to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.
⭐For copyright or any inquiries, please contact us at zh@codinghelmet.com

Наука

Опубликовано:

 

3 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 55   
@zoran-horvat
@zoran-horvat Год назад
Become a patron and get access to source code and exclusive live streams: www.patreon.com/posts/what-is-and-in-c-81378570
@TrungTran-rz6tz
@TrungTran-rz6tz Год назад
I felt off my chair when he said "it is very simple indeed" at the end
@zoran-horvat
@zoran-horvat Год назад
I couldn't resist, sorry :)
@dererzherzog
@dererzherzog Год назад
Hahaha, me too! But admittedly, it did get a lot less confusing after this video
@patricknelson
@patricknelson 5 месяцев назад
Ok, so… I wasn’t the only one. 😂
@VincentYang024
@VincentYang024 Месяц назад
I have to refresh this whenever I want to use these terms to describe something to others😂
@phyberoptyk5619
@phyberoptyk5619 2 года назад
You sir never cease to blow my mind with your incredible insight. Thank you!
@piotrrybka318
@piotrrybka318 26 дней назад
Very good explanation, but not because of the examples, but for the initial introduction of the cause-effect origins of both terms. I think this is the actual eye-opener which explains the essence of both concepts. I would only emphasize as strongly as I can that co- and contravariance are both very general concepts, and it's not about C# interfaces, in/out keywords, parameters and return values. The examples you gave may have been switched or given in another language, but the general idea is the following: If our new type N that we are building (the interface in your example), follows the order of derivation strictly, then it's COvariant, otherwise, it's CONTRAviariant. To put it more clearly: if there is type B derived from A, so B:A and I have a generic type N, then if for N I can get (in whatever way) only A in other words for shrinking⬇ of N, the output is also shrunk⬇ and for N I can get both A and B in other words, for expanding⬆ of N, the output is also expanded⬆ then N is COvariant if for N I can get both A and B in other words for shrinking⬇ of N, the output is expanded⬆ and for N I can get only B in other words for expanding⬆ of N, the output is shrunk⬇ then N is CONTRAvariant. But of course, it's still nice to see an implementation, which although not fully developed (you're assigning null! to your interfaces and try to use them), could easily be used in a compilable code.
@dracuul78
@dracuul78 Год назад
Thanks! This has to be the best explanation of the topic I've come across, with a nice exhaustive set of examples.
@zoran-horvat
@zoran-horvat Год назад
Thank you! I am glad to hear it was helpful.
@davidpccode
@davidpccode 4 месяца назад
This explanation is not only perfect, it is a work of art.. Thanks!
@gorotim2000
@gorotim2000 Год назад
Деда, ты лучший. Я понял разницу между этими принципами, благодаря тебе.
@dererzherzog
@dererzherzog Год назад
A great and very systematic explanation of the concepts of Covariance and Contravariance and their practical implications in various contexts (reference assignment, method parameters and interfaces).
@learner8084
@learner8084 Год назад
Zoran sounds like a very intelligent person. Thanks for the video. Never x my mind to look at Base and Derived this way !
@Rhysling2
@Rhysling2 Месяц назад
Excellent explanation. I’ve heard several and this is the best. I may actually understand cov/ contra now.
@NoDoubt747
@NoDoubt747 Год назад
Great job! That was the best demo on this topic I've found!
@Darebit2k1
@Darebit2k1 2 года назад
¡Gracias por compartir!, los ejemplos estuvieron muy claros y despejan toda duda acerca de estos dos conceptos
@muhammedalikhan7559
@muhammedalikhan7559 4 месяца назад
Amazingly simple explanation. Thank you so much for this!
@mumk
@mumk Год назад
you made it so so so so so clear on this topic, thank you sir!
@alecmatias4774
@alecmatias4774 Год назад
Thanks! This explains it better than other tutorials.
@andreapulvirenti
@andreapulvirenti 7 месяцев назад
Super clear video! thanks!!
@user-tk2jy8xr8b
@user-tk2jy8xr8b Год назад
The terms may be actually coined from math, there are covariant and contravariant functors in category theory and they very nicely demonstrate whan happens in C#. A covariant functor f (or just "functor") has a mapping `(a ~> b) -> (f a ~> f b)`, a contravariant functor g has a mapping `(a ~> b) -> (g b ~> g a)`. Now, imagine that ~> stands for "is subtype of", `f` and `g` are your types with variance definition on their type parameters, and you get variance relations in OOP: from `A : B` follows `F : F` if `F`, from `A : B` follows `F : F` if `F`. Now, about those `in` and `out`: functions are functors, covariant in their output and contravariant in their input.
@Wilhelm_LAS
@Wilhelm_LAS 4 месяца назад
bro is a walking mathematic
@purplebytessolutions6994
@purplebytessolutions6994 5 месяцев назад
Beautifully explained!
@imaginative-monkey
@imaginative-monkey 2 месяца назад
Yeah, it was so easy, but for some reason, it was the first time I could understand it! 😆 👍
@banster85
@banster85 10 месяцев назад
Great and very clear explanation, thanks!
@brenodev
@brenodev 3 месяца назад
Best explanation ever. You're the man! Thanks Zoran :)
@user-we6wp1ky7f
@user-we6wp1ky7f Год назад
Very clear explanation of this difficult concept!
@muradshirinov4090
@muradshirinov4090 Год назад
Very good explanation , thank you
@somayehsheikhi7839
@somayehsheikhi7839 Год назад
Wow Thanks a lot Zoran, Finally I could understand these concepts. you are amazing .
@almajdzino2365
@almajdzino2365 Год назад
Very clear tysm!
@awazin4031
@awazin4031 Год назад
Wow ! Amazing teacher !
@zoran-horvat
@zoran-horvat Год назад
Thank you!
@montrazvan89
@montrazvan89 Год назад
Thank you guy from Budapest! Szio!
@aj.arunkumar
@aj.arunkumar 4 месяца назад
explained perfectly.. i m checking out your course in pluralsight
@user-st6xe3lp5p
@user-st6xe3lp5p 5 месяцев назад
For anyone who is new to reference variable, here's an explaination OF MINE (enough for me, BUT i'm not sure 100%): Base x = new Base(); means x is NOT the object, x is just a reference variable which holds the reference to the location in memory of the object of Base class created by the new Base() , x has Base type, of course. Base y = new Derived(); means y is reference which holds the reference to the location of the Derived's object which is created by the new Derived() , y has type Base and for Base is father of Derived so y can hold the reference to the location of Derived's object AND y CAN ONLY access overriden stuff in Derived.
@BeatusCanis
@BeatusCanis Год назад
Thank you for the good course. However, I would suggest insisting on what the in/out keywords do and how that ties into object substitution. I had to go to another source to fill in the details of co/contra-variance.
@zoran-horvat
@zoran-horvat Год назад
Thanks for the suggestion. That makes sense.
@bitmanagent67
@bitmanagent67 8 месяцев назад
My brain just froze. Rebooting.
@kaizer-777
@kaizer-777 6 месяцев назад
One thing that might not be immediately obvious is that you can't nest the generic types and get the same behavior since T includes the fully nested type. So if you had a List you couldn't assign a List to it.
@zoran-horvat
@zoran-horvat 6 месяцев назад
That is because List is invariant. Try with IEnumerable.
@anywheredoor4699
@anywheredoor4699 Год назад
Is there something on a thorough understanding of the TPL and also the difference of concurrency and parallel processing
@winzyl9546
@winzyl9546 9 месяцев назад
I understood Liskov Substitution principle, but I didn't understand what in or out does and how it relates to co or contravariance.
@imaginative-monkey
@imaginative-monkey 2 месяца назад
One big question is: if for producer methods, the correct behavior is "Covariance" and for the consumer methods, the correct behavior is "Contravariance", why is it the developers' responsibility to set it?! I would like to know the applications. In other words, whenever I'm defining a generic type, should I go through the decision-making process of variance options? I've hardly encountered a code base that had specified this. Does it mean it's majorly used internally in .NET code base? Thanks! 👍
@zoran-horvat
@zoran-horvat 2 месяца назад
Variance tells the compiler how to verify assignments. It appears that definite checks were not possible to automate, and so the language designers left it to the programmer to specify. I am not aware of any strongly typed language that is doing that automatically (though, I would love to learn if one exists!). That would be an interesting achievement.
@imaginative-monkey
@imaginative-monkey 2 месяца назад
@@zoran-horvat Thanks for the reply. I hope some analyzers could provide suggestions like "You're just using T to return objects, would you like to mark T as covariance?"...
@ozziew007
@ozziew007 11 месяцев назад
Eureka moment. The concept is actually not that hard to understand when someone actually explains it well
@TrungTran-rz6tz
@TrungTran-rz6tz Год назад
I have this analogy, hope it helps somebody to grasp the concept easier Imagine you're a producer, if your product's quality improves you can satisfy more customers, more quality = more customers, so you are *covariant Imagine you're a consumer, if your expectation in product quality goes up there'll be fewer products that can satisfy you, more expectation = less options, so you are *contravariant
@STUPIDYOUTUBE_AI
@STUPIDYOUTUBE_AI Год назад
I got lost...😰
@zoran-horvat
@zoran-horvat Год назад
Watch again after a few weeks pause, and don't forget to write that code in the IDE as you progress. That will finally teach you and, once variance gets into your mind, I'm sure you will never watch the assignments the same way again.
@Marfig
@Marfig Год назад
I'm sorry, but you don't explain the need for the in and out keywords, as all your syntax checking remains the same when you take them out from the interface declarations. It's only at 9:10 in your video that the real importance of the keywords is revealed. But you never explain it, nor its practical implications. You only make statements in your video, you don't explain anything.
@zoran-horvat
@zoran-horvat Год назад
Excuse me, I must object to that. What was I doing for ten minutes if not explaining? But anyway, I have watched the video for you, and pulled out the important parts, so that you can focus on separate explanations as per need. 04:00 - 05:00 Announces variance as augmenting the object substitution principle 05:00 - 05:40 Explanation of out and in keywords, when they are used and what do we call that 05:40 - 07:40 Practical implications of the out keyword, applying the OSP to output values 07:40 - 09:05 Practical implications of the in keyword, applying the OSP to input values 09:10 - 11:05 Practical implication of OSP when applied to variant interfaces
@Marfig
@Marfig Год назад
@@zoran-horvat I'm sorry. We'll just have to disagree on this. It's not my intention to deride you. But I felt that nowhere in the video you explain the need for those keywords. You state the syntax and grammar of the language very well. But come short on the semantics. As an example, You didn't remove the in and out keywords from the interfaces and explained the compiler syntax check changes that causes and why. This would have been the aha! moment for someone like me, who was trying to understand the concepts. I saw your video first yesterday. And remained confused as to why I needed those keywords. It was only after experimentation and seeing another video from someone else that I finally understand the concept and its applicability. Sorry.
@alexeybeloushko7240
@alexeybeloushko7240 3 месяца назад
Covariance and Contravariance names comes from simple logical diagram of implicid assignment direction rules Contravariance PClass Generic Covariance PClass
@zoran-horvat
@zoran-horvat 3 месяца назад
You are right about the analysis, but the use of names in science for the same effects is predating their use in programming by decades.
Далее
Covariance and Contravariance
13:31
Просмотров 11 тыс.
Object-Oriented Programming is Bad
44:35
Просмотров 2,3 млн
Main Hall 06: Why is Functional Programming so hard?
37:44
C# Covariance
17:27
Просмотров 7 тыс.
Faster than Rust and C++: the PERFECT hash table
33:52
Просмотров 520 тыс.
Master the Design of Functional Types in C#
17:53
Просмотров 13 тыс.
17 Pieces of C# Syntax That Make Your Code Short
12:41
PA-RISC рабочая станция HP Visualize
41:27