C# Covariance

preview_player
Показать описание
Coding Tutorial: Is a bag of apples a kind of bag of fruit? It all comes down to covariance.

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

This video is so underrated. finally a great explanation of covariance that I could understand.

mohitkumar-jvbx
Автор

Best tutorial I have seen on covariance. This deserves much more attention.

Rhysling
Автор

Man... Just found your channel and your explanations are so clear and concise, hope you get millions of subscribers... Keep up with the great job! Thank you sir!

jheysonlima
Автор

i lucked out, this is the first video i watched and i kind of got it the first time, when watching a second time it made even more sense. thanks.

stinknay
Автор

incredible! never had understood these over years.

TheJessejunior
Автор

Theeee best ever covariance explanation!

thRWn
Автор

This video got me more clear about covariant in c#, now i know the real reason why Microsoft adding that feature to c#.

peternguyen
Автор

You have two Synchronous Yield Return in Collections playlist and no C# Contravariance

vladmaiorov
Автор

I read the msdn docs, saw other vídeos and couldn't understand, with you i finally got It, thanks!

But would you mind giving an example of a real world use of covariance?

paulofernandoee
Автор

Wonderful video which explains the concepts so well! Thank you!
I have a couple of comments:-
1) I got very confused on how you could add a banana to the bag of apples. Took me a lot of time to figure out the base class gets called. If you can draw the reader’s attention to this, it will be very useful as this polymorphism/ inheritance sometimes trips me up.
2) Is there a need to declare the Fruit class as Abstract?

padmakrishnan
Автор

Very clear tutorial, thanks for the explanation!

FerdieSwinkels
Автор

At 6:33why can you add a banana to the bag of apples? Didn’t you overwrite the add method with the type of apples? Or does it not overwrite because of overloading? (I am a JavaScript programmer trying to learn C#)

DedicatedManagers
Автор

Hello! when you say " there is an inheritance relationship between IEnumerable<Fruit> and List<Apple> by adding the Out keyword, so when we add the Out keyword to the interface behind the scene the compiler will "understand" List<Apple> implement the interface IEnumerable<Fruit> ? Or because the IEnumerable<Fruit> is just the read-only inteface and it is safe in this case so the compiler allow us to do so? Thanks so much.

peternguyen
Автор

That code below is also covariance? Everyone mentions only generic interfaces, delegates, array examples.
Employee emp = new Manager();
Or
static void Work(Employee e)
{
switch(e)
{
case Manager m: m.DoManagerWork(); break;
case Developer d: break;
case Employee emp: break;
}
}

iAndrewMontanai
Автор

7:00 I am a bit confused. Shouldn't it fail right away when we add a banana to BagOfApples? Add method expects an instance of Apple. And Banana class is not derived from Apple. So, even though the underlying structure - List<Fruit> can store any kind of fruit, it appears that the Add method's parameter type should be enough to act as the gatekeeper here.

piotrjan
Автор

I still could not get it from the business reasoning standpoint: why building such perversive multi-layered abstractions? what's the point besides that one programmer could do something wrong (there are thousands ways we could f..kup here and there - but thats why tests were invented) ... and to prevent it other programmers invented other strange things ... that later could lead for yet other programmers (who struggle with these perversive abstracts) to introduce new bugs due to complexity constructs now being actively accepted into mainstream (because every other dev wants to show he is cool too, or just smarter and thus irreplaceable, or needs a payrise)
... what's wrong with just sticking to the KISS principle? - the code would be 10-15...50% longer (although more humanly readable)? - ok, let it be; to me this seems to justify avoidance of risks of complexity and dangerious bugs it brings manyfold. I am 40+ (so have some life understanding and managerial experience) and I come from the business side of the world. Though I'm still a noob in c# (but keep trying) learning it for some time - but can't throw away a strong impression that business overpays the IT world substantially not just because of hype, but also for some internal competition and smartass tactics programmers apply within the community which reflects on the code and IT products too; and imho this needs to be stopped untill its too late :)

sergeyt
Автор

why can't someone simply explain covariance without all these convoluted concepts.

auronedgevicks