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

preview_player
Показать описание
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 YouTube.❤️

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.🔥❤️

⭐ Learn more from video courses:

▬▬▬▬▬▬▬▬▬▬▬
⚡️ Have a look at our other Videos :

👉 Other videos on this channel you may be interested in watching:

⚡️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 📱👨

▬▬▬▬▬▬▬▬▬▬▬
👨 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 YouTube 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.

Рекомендации по теме
Комментарии
Автор

I felt off my chair when he said "it is very simple indeed" at the end

TrungTran-rztz
Автор

You sir never cease to blow my mind with your incredible insight. Thank you!

phyberoptyk
Автор

Thanks! This has to be the best explanation of the topic I've come across, with a nice exhaustive set of examples.

dracuul
Автор

Деда, ты лучший. Я понял разницу между этими принципами, благодаря тебе.

gorotim
Автор

This explanation is not only perfect, it is a work of art.. Thanks!

davidpccode
Автор

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).

dererzherzog
Автор

Thank you! very clear, and I love your talking speed, it makes me easy to follow

nightwheel_c
Автор

Zoran sounds like a very intelligent person. Thanks for the video. Never x my mind to look at Base and Derived this way !

learner
Автор

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<T>, then

if
for N<A> I can get (in whatever way) only A
in other words for shrinking⬇ of N, the output is also shrunk⬇
and
for N<B> 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<A> I can get both A and B
in other words for shrinking⬇ of N, the output is expanded⬆
and
for N<B> 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.

piotrrybka
Автор

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<A> : F<B>` if `F<out T>`, from `A : B` follows `F<B> : F<A>` if `F<in T>`. Now, about those `in` and `out`: functions are functors, covariant in their output and contravariant in their input.

tkjyxrb
Автор

Excellent explanation. I’ve heard several and this is the best. I may actually understand cov/ contra now.

Rhysling
Автор

Amazingly simple explanation. Thank you so much for this!

muhammedalikhan
Автор

What advantage gives using "MyInt<out T>" versus "MyInt<T>" not specifying anything? Because if it doesn't change the behavior then it can be just ommited always, right?

Автор

Thanks! This explains it better than other tutorials.

alecmatias
Автор

Best explanation ever. You're the man! Thanks Zoran :)

brenodev
Автор

you made it so so so so so clear on this topic, thank you sir!

mumk
Автор

Yeah, it was so easy, but for some reason, it was the first time I could understand it! 😆 👍

imaginative-monkey
Автор

Wow Thanks a lot Zoran, Finally I could understand these concepts. you are amazing .

somayehsheikhi
Автор

¡Gracias por compartir!, los ejemplos estuvieron muy claros y despejan toda duda acerca de estos dos conceptos

Darebitk
Автор

Great job! That was the best demo on this topic I've found!

NoDoubt