How To Create Generics in C#, Including New Features

preview_player
Показать описание
Ever since Microsoft introduced generics into the language, they have been used almost everywhere. Even if you don't understand how they work, using generics is rather easy.

In this video, we are going to look at why generics are important and then how to create them. We will look at the different types of generics, how to limit generics, and how to implement some of the more recent generic types, such as generic interfaces and numeric generics.

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

It would be so interesting to see a "real world" scenario with interfaces of generic types being implemented, love this stuff

clarkflavour
Автор

To date my experience, I was so happy when generics were added to c#. I came from c++ and it was frustrating not having a template equivalent.

runtimmytimer
Автор

Tim, the best as always. This is a masterpiece!

austinmudadi
Автор

I love the trick in generics where you use constraint the type to the current class e.g. `BaseClass<T> where T : BaseClass<T>`. It looked weird at first, even now it is still weird, but is very useful so that the base class can get the type of the derived class. This mechanism is used in heavily in generic maths.

astralpowers
Автор

Thank you very much Tim. I was really struggling to uderstand generics until I found this video. In general I adore the way you deliver knowledge, keep up the good work!

neman
Автор

Fantastic timing. I'm just trying to break down an existing Generic class to figure out how and why it's being used.

leppyr
Автор

IAmTimCorey, this is very great video covering all the basics of generics. Unfortunately, I didn't learn anything new. I will definitely recommend it to my friends who don't understand this topic yet. Can you please make a video with more advanced things like <in/out T> and maybe Monad-like classes with embeded logic and conversion?

efimov
Автор

I actually had one case where I started out with interfaces but had to change to using generics that implemented the interface. The reason was sometimes whoever uses it needs to know the full type with all methods, not just what the interface used and worked out great.

andywalter
Автор

Always spot on - nice use of C# features as well :)

MalcolmSwaine
Автор

Great video! Thank you Tim! Much appreciated for your time on this.

atrave
Автор

always a pleasure watching your videos while doing some cardio training like right now :-)

michaelglockenmeier
Автор

It was a complete explanation of using Generics in C#. Thank you so much.

SagharMax
Автор

What an awesome lesson! Actually it was a story about benefits of parametric polymorphism instead of an ad hoc polymorphism which was also shown in this video with overloading of methods.
But can you please tell me Tim if we can change a HUGE switch with one generic method? The problem is that the behaviour should be different depending on what T is. Of course I can make a ton of if statements inside of the above-mentioned generic method checking what T actually is but it will be almost the same pattern matching as having a huge switch. Or I can make an interface and implement it in my class containing this generic method and implement this interface many times in the same class where T is going to be a different types I need (that is what I usually do with IMessenger in Avalonia). Feeling confused a bit. What would you do in such case Tim? I am pretty sure that polymorphism is the answer but how it can be implemented in such case of a huge switch I don’t understand unfortunately…

torrvic
Автор

Your last few mins of video were excellent, I somehow forgot the importance of `where`. Thanks a lot.

vibes
Автор

Hi Tim, thank you very much. Good explanations but i´m missing a lot of stuff. How to declare a method that returns a value of <T>? How to deal with type conversion inside a method if e.g. math operations lead to "can´t implicit convert from int to <T> or vice versa. Definitely needs a part 2. 🖖

acerreteq
Автор

Your knowledge is truly inspiring. The amount i gained from your video is incredible.

andergarcia
Автор

Very helpful! Thanks for doing what you do🙌🏻

trudyreiser
Автор

Thank you Tim once again!! Very powerful videos, knowledge is the key in development! My question is can i pass Generics inside a controller for aspnet core?

Dimitris.Christoforidis
Автор

PersonRecord + PersonRecord might sometime produce MarriageRecord, or even ChildRecord (derived from PersonRecord), but most often they just produce an instance of PeopleRecord.

Sanabalis
Автор

Aside from what you mentioned about object type being more expensive than the generic T type, it might be worth mentioning that while the object type allows you to shove everything and anything into a list the generic T type allows only one.
I know you mentioned this but it might not have been explicit to someone that's never dealt with generics before.

fabio.albuquerque
visit shbcf.ru