Delegates in C# - A practical demonstration, including Action and Func

preview_player
Показать описание
Delegates in C# are really powerful, but most people don't know how to use them effectively, if at all. Today I am going to show you what delegates are, how to use them, why they are useful, and really how to take your code to the next level. Along the way, I will cover the special types of delegates named Func and Action. Now if all these new words seem intimidating, don't worry. This topic is actually really simple at its foundation. Yes, you can do some complex things with delegates, but an actual delegate is absolutely simple.

0:00 - Intro
1:28 - Demo application walk-through
7:52 - Explaining and creating a Delegate
18:19 - Func and Action: problems you can solve with delegates
21:37 - Func Delegate: creating and explaining
30:56 - Why to have delegate, if all the work is done elsewhere
34:00 - Action Delegate: creating and explaining
38:07 - Creating anonymous methods: anonymous Delegate
43:08 - Creating anonymous methods: anonymous Func
45:15 - Creating anonymous methods: anonymous Action
49:30 - Using Delegates in other projects: WinForms
58:37 - Using Anonymous Delegates in other projects: WinForms
1:02:15 - Summary
Рекомендации по теме
Комментарии
Автор

This is for me:

Example: 1:50
What is a delegate?: 7:52
Func delegate: 18:19
Question: 30:55
Action delegate: 34:00
In line delegates (on the fly): 38:04
WinForm UI: 49:22
WinForm UI with In line delegates: 58:37
Ignoring Action delegate: 1:00:52
When using delegates?: 1:02:15

kerotan
Автор

No BS, I watched this video 5 days ago and used my knowledge to answer an in person interview question today!! Thanks Corey!

russellkemmit
Автор

that moment when you've been watching for 40 minutes and he says "in a nutshell" 38:03

baltarifcan
Автор

I recently switched companies and therefore my Java hat to a C# hat.
The quality and information these videos provide is insane. Keep up the good work!

sparrow
Автор

Welcome to OO programming ...we fuck things up and then come up with over-complicated solutions to solve them

christianrazvan
Автор

Ah delegates, the worst topic in C# history! Thank you Tim for covering it.

LuigiZambetti
Автор

Okay, you are officially the first and only good tutorial I have ever seen on delegates. My issue has always been "Why?". Because I understand what delegates do but never understood why I would go through the trouble of setting them up. This shed light on everything! You're the only one to show how delegates can essentially carry the value of a method and make it transferable to different UI's without having to change very much.

Thank you so much for putting the time into these videos! I hope I can be a better developer by using this to my advantage because not even people I work with could give me this explanation.

CasshernSinz
Автор

Its the hardest thing about learning new or advanced areas in coding, is that the simplicity of the example environment, obscures the usefulness of the new tool.

titanicpat
Автор

Sir have you covered Entity Framework in your videos?

mmuneebajaz
Автор

Thank you so much for all the hard work you put into making this content. Seriously one of the best, if not THE best C# tutorials I've found so far.

Ravroid
Автор

Understanding the concept is easy but actually thinking "I could use a delegate for that" while coding, this needs a lot of experience. The problem: as long as you don't use it you will forget it. I'm kinda stuck now.

das_evoli
Автор

Tim Corey is a national treasure. Prove me wrong.

rossthemusicandguitarteacher
Автор

- The best explanation of Delegate.
- The best part of this video is that the focus is on details and that is where the devil lies.
- Keep up the good work Tim Corey. :)

faizalvasaya
Автор

Delegates seem like a strongly-typed version of JavaScript callback functions. Would that be a fair comparison?

pierreplourde
Автор

This tutorial is better than the one on pluralsight for sure. However, it's still a little confusing having three different types of delegates passed into a single function, especially as a tutorial. I also noticed a few times you mentioned this isn't how we would actually want to do it, which is usually not something you should be showing people who have absolutely no idea what delegates are. I like to learn best practices, and while this isn't a tutorial on best practices, I want to see delegates used in cases where it makes sense to use them, where they are one of the best solutions for a particular problem.

I really would have liked to see some cleanup, instead of using 3 delegates, clean it up and just use 1.

bobthebuilder
Автор

Only 16 arguments!?! Oh no!!!! It's Y2K all over again! :-) :-) :-)

PerryCodes
Автор

Finally I have started to understand delegates and their usage. Thanks Tim!!

vartikagupta
Автор

Thank you TIm, for these awesome videos. I can learn anything in C# with these videos.

miladabdi
Автор

Thank you! Great tutorial. Clear explanations :-)

raphaelpinel
Автор

Most of the videos does not explain the concept, and they just start coding.
Delegates provide ability to pass methods as parameters to other methods. So that other methods can execute them. In Javascript functions are objects. So passing a function as parameter to other methods is not a problem. But in C#, you must add the function reference to a delegate object, and then it can be passed to another method, which can execute it as and when needed. Most popular use of delegates is in implementing callback methods or event handlers. C# EVENTS use delegate internally to implement events and event handlers.
The PUBLISHER class declares a delegate, and then an event which uses the delegate internally. Then raises the event as and when needed.
The SUBSCRIBER class subscribes to the event of publisher. Internally, it just adds the EventHandler method reference to the delegate of SUBSCRIBER. That is the whole story.

BahawalTV