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

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ย. 2024

ความคิดเห็น • 756

  • @russellkemmit73
    @russellkemmit73 5 ปีที่แล้ว +39

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

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +6

      Awesome! Good luck on landing the job. Glad I could help.

  • @sparrow5813
    @sparrow5813 3 ปีที่แล้ว +12

    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!

  • @darthvader_
    @darthvader_ ปีที่แล้ว +3

    Tim, you have completely changed the way I look at Delegates. The tutorial was easy to understand and I can't thank you enough for the kind of example that you picked up. I have went over numerous videos on this topic and I'm relatively new to C# but your presentation skills were so to the point that I was able to bridge the gap pretty easily. Please continue making great tutorials like these! I kept on running from delegates before watching this video

    • @IAmTimCorey
      @IAmTimCorey  ปีที่แล้ว +1

      I am glad it was helpful.

  • @rossthemusicandguitarteacher
    @rossthemusicandguitarteacher 3 ปีที่แล้ว +3

    Tim Corey is a national treasure. Prove me wrong.

  • @lazarnikolic8161
    @lazarnikolic8161 4 ปีที่แล้ว +2

    Comparing delegates and interfaces is one of the best explanations I've come across so far.

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว

      Glad it was helpful!

  • @vin2629
    @vin2629 ปีที่แล้ว +2

    This must be the best video on delegates I came across so far.

  • @ehsanshirazi2003
    @ehsanshirazi2003 2 ปีที่แล้ว +2

    The last part that winform was connected to the function that uses delegate was the best part. Thank you for your contribution.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว

      Glad it was helpful!

  • @vartikagupta8816
    @vartikagupta8816 3 ปีที่แล้ว +4

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

  • @maxlin2905
    @maxlin2905 3 ปีที่แล้ว +1

    Hi Tim, just let you know that your lessons are becoming an important part of my life, I spend 2 or 3 hrs on listening to them and I love them. Thank you!

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      Awesome! I'm glad you are finding them valuable.

  • @ayam_jantan_
    @ayam_jantan_ 9 หลายเดือนก่อน +2

    this video is the ULITIMATE GOAT! thanks Tim, you did it again.

    • @IAmTimCorey
      @IAmTimCorey  9 หลายเดือนก่อน

      I am glad it was helpful.

  • @xFieRZex
    @xFieRZex 2 ปีที่แล้ว +1

    I've read about delegates in Shildt's book, then in Troelson's book and finally only after this amazing tutorial I get it. Thank you so much!

  • @JackNotJack
    @JackNotJack ปีที่แล้ว +2

    Another great tutorial. I'm still a little nervous around delegates and anonymous methods, but going through this tutorial helped so much. Thanks Tim.

  • @Jose67898
    @Jose67898 2 ปีที่แล้ว +1

    Very good explanation. The conclusion for me (in this concrete case):
    1. GenerateTotal can send "messages" to Main while it process its work.
    2. I can write the code for some operation GenerateTotal does.
    So GenerateTotal is just a plan, what have to do, but the concrete code is mine.

  • @kerotan2444
    @kerotan2444 4 ปีที่แล้ว +25

    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

  • @xianyu6565
    @xianyu6565 4 ปีที่แล้ว +1

    Come back to say Thanks!! I used the knowledge gain here in my actual work today! Very excited!!

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว

      Fantastic! I am glad it was helpful.

  • @bubblesgrappling736
    @bubblesgrappling736 4 ปีที่แล้ว +1

    once again, tim corey had the best video on the topic

  • @delhitepawan5804
    @delhitepawan5804 3 ปีที่แล้ว +2

    Excellent Tim...keep the good work going.

  • @guyprovost
    @guyprovost 5 ปีที่แล้ว +1

    Just want to let you know that if you go to around 50 minutes in the video, you seem to repeat a section where you present the startup code, like if you notice that you already had a private static void already written. I think you forgot to edit it out!
    Just want to let you know, because looking at the quality of your videos, it shows that you put a lot of time in it and that I put the time to watch it very carefully!
    Great... Great content, thanks for sharing!

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +1

      That could be and I just missed it when doing my edits. Glad you enjoyed it anyway.

  • @shayak9048
    @shayak9048 5 ปีที่แล้ว

    That's an excellent way of teaching. It's much easier to understand like this instead of reading the concept in book. :)

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +1

      I am glad you are finding it enjoyable.

  • @furiousmatthew2287
    @furiousmatthew2287 2 ปีที่แล้ว +3

    you're a genius men, seriously! :) I subscribe instantly

  • @jakoblindblad191
    @jakoblindblad191 4 ปีที่แล้ว +2

    Holy fk if i had u as my teacher in school i would have called Bill Gates my employee by now. Keep up the good work.

  • @extremedrone5365
    @extremedrone5365 2 ปีที่แล้ว +1

    I recomend to move the CalculateMethod to the Library project rather than implementing it in Console and WinForm projects. But thanks for the class. It is awesome I learned a lot

  • @andywalter7426
    @andywalter7426 4 ปีที่แล้ว +1

    I would suggest doing a video about delegates vs interfaces and when to use each one. I have actually found many cases where doing interfaces caused overflow errors but delegates did not. Does that mean that interfaces are completely useless and a person should always do delegates. Would like to know the advantages and disadvantages of interfaces vs delegates.

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +2

      Delegates and interfaces aren't really comparable items. Interfaces are just contracts that say that a class will do whatever the contract specifies. This allows us to work with classes that otherwise have no relationship. For instance, you can apply an IDisposable on any class and use it in a "using" statement to properly close out the resources at the end. Those classes can be anything and yet they can all be properly closed out. Quite frankly, Interfaces are probably the most important thing in C#. Without them, there is little point to dependency injection, unit testing is practically impossible (or at least miserable), and code in general becomes much more difficult to manage. I find myself using delegates (beyond events) rarely in my code. I use Interfaces all of the time.

  • @ShyamSundar055
    @ShyamSundar055 3 ปีที่แล้ว

    Its really "in a nutshell" about delegates. Great stuff 👍

  • @brightndiweni1657
    @brightndiweni1657 6 ปีที่แล้ว

    I am enjoying even the comments, great discussions here awesome environment. Cnt wait for the next one!!!!

  • @pavolbutora
    @pavolbutora 3 ปีที่แล้ว

    Again, excellent and simply explained. Thank you.

  • @a.porteghali7402
    @a.porteghali7402 3 ปีที่แล้ว

    Hi Tim. Well, after months since the first time I watched this, I came back to watch it again. There are points in your course that could be easily missed unless someone came across with in real world. I'm getting used to the delegates (Func and Action actually) when starting separating some codes into libraries. You mentioned the interface at the beginning of this video. I think both interface and delegate are sort of concepts which a programmer need change his/her mindset. I don’t know; however, I believe their application in a code is a big step getting away from classic programming style, otherwise something always whispers in the ears what is the point of using them when things can be done without them. (a false statement of course :) )?
    Thank you again.

  • @LuigiZambetti
    @LuigiZambetti 6 ปีที่แล้ว +6

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

    • @chezchezchezchez
      @chezchezchezchez 6 ปีที่แล้ว +2

      Luigi Zambetti why is it the worst topic?
      You make a powerful statement like that, can’t even say why?

    • @gunhound45
      @gunhound45 6 ปีที่แล้ว +1

      Jim Cheseborough They are hard to understand for beginners. Like pointers in C

    • @christianjamesguevarra6257
      @christianjamesguevarra6257 6 ปีที่แล้ว +3

      Luigi Zambetti just think of delegates as actions or "verbs" being treated as nouns or objects that you can pass around like a normal variable

    • @IAmTimCorey
      @IAmTimCorey  6 ปีที่แล้ว +5

      Couple factors here that make things hard for newer developers especially. First, it is a confusing name. The name doesn't make the topic clearer. Second, it involves executing code elsewhere, not inline. That makes it harder to read. Hopefully this video cleared some things up.

  • @RalfsBalodis
    @RalfsBalodis 4 ปีที่แล้ว +2

    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

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +1

      Thanks! I added it to the description.

    • @DoorThief
      @DoorThief 4 ปีที่แล้ว

      @@IAmTimCorey You sure did. Typo and all! lol
      Through*

    • @RalfsBalodis
      @RalfsBalodis 4 ปีที่แล้ว

      @@DoorThief Thanks. English is not my natural language. I try spellcheck everything thou. I slipped up. Sorry.

  • @offichat6054
    @offichat6054 6 ปีที่แล้ว

    It would be really helpful if you could make a video about abstract classes. So far you have only covered interfaces and it would help to clear the confusion when it's better to use an interface or an abstract class.

    • @IAmTimCorey
      @IAmTimCorey  6 ปีที่แล้ว +1

      That is on the schedule to do soon. Good suggestion.

  • @mtsacchi
    @mtsacchi 3 ปีที่แล้ว

    Great video, Tim!
    Already subscribed to you blog. I'll take a deeper look into the courses you sell.
    Thanks a lot!

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      You are most welcome. Thanks for watching.

  • @romannice4572
    @romannice4572 4 ปีที่แล้ว

    Thank you for this! Im not a native speaker but these tutorials are very easy to follow 👍.

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว

      Awesome!

    • @davidbritz8548
      @davidbritz8548 4 ปีที่แล้ว

      @CloroxBleach then maybe you can explain it to me :)

    • @davidbritz8548
      @davidbritz8548 4 ปีที่แล้ว

      @CloroxBleach then maybe you can explain it to me :)

  • @markbell2037
    @markbell2037 3 ปีที่แล้ว +3

    You mentioned making a video on async safe delagates....Have you made that video yet, and which one is it?

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว +2

      Not yet!

    • @markbell2037
      @markbell2037 3 ปีที่แล้ว +1

      @@IAmTimCorey Thanks for getting back with me, I will try to be patient!!

  • @densely123
    @densely123 3 ปีที่แล้ว

    I really like doing something with c#.
    You are So cool.
    Thanks you.

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      You're very welcome!

  • @twentytwenty8749
    @twentytwenty8749 6 ปีที่แล้ว +1

    Good voice, pace, etc., and very glad you are here.
    This video should be reshot posthaste. The use of excruciatingly awkward method names and nonsensical calculations make it hard to track and remember what one has learned.
    While I got, because of previous experience, the gist of the advantages of delegate usage (console vs. WinForm output is a good example) and delegate usage is an elevated (and intrinsically cryptic-looking) textbookish technique, the user may ask, "Rather than the oddball gymnastics of delegates, why not use an abundantly clear IF statement that branches on console or UI?"
    The takeaway on FUNC and ACTION was lost. Why would I bother with these? What are they wrapping? Is Santa still coming? Totally lost.

    • @IAmTimCorey
      @IAmTimCorey  6 ปีที่แล้ว +1

      The issue of delegates is who creates the method. The examples I gave with WinForm vs Console illustrate it but they would not be solved with a simple if statement. If you tried an if statement, it would work until you tried to use the library in ASP.NET MVC. Then you would have to update the library and redistribute it again with new logic for that UI. Then you would have to do it again for ASP.NET Core or Xamarin or any other UI. By using a delegate, we can put in the UI logic into a class library that does not change. When we add the UI, we add the UI logic. That allows us to follow the Open Closed Principle (OCP) and it allows us to more easily maintain our library and our applications.

    • @twentytwenty8749
      @twentytwenty8749 6 ปีที่แล้ว

      Yikes. Well, so much for formatting. :-)

    • @twentytwenty8749
      @twentytwenty8749 6 ปีที่แล้ว

      P.S. I listened again to the FUNC and ACTION portion, and I think I get it now. First run through, the naming was throwing me off. Rinse, lather, repeat. :-)

  • @martin6778
    @martin6778 2 ปีที่แล้ว +1

    It's very Great Explanation.. wonderful, Thx a lot sir

  • @RobertoLion
    @RobertoLion 2 ปีที่แล้ว +2

    At 1:04:35 shouldn't it be "Dashboard inherits from Form" instead of "Form inherits from Dashboard"?

    • @shaunhunterit342
      @shaunhunterit342 2 ปีที่แล้ว +1

      Yes that is clearly what he meant to say

  • @pablocaiza.
    @pablocaiza. 3 ปีที่แล้ว

    thank you for all your hard work on this

  • @harshitmittal1995
    @harshitmittal1995 ปีที่แล้ว

    So basically Delegates we use to get parameters for our method right????...... BTW thank you Tim you are just amazing ....from this one video I mastered delegates.

  • @drip888
    @drip888 2 ปีที่แล้ว +1

    Tim, when i use C2, i get the thing a bit differnet.
    like as of now 17:10 i get an output like
    The subtotal is ? 22.93
    The total for the cart is ? 20.64
    i am not getting the dollar symbol. Why does that happen? How to overcome it?

  • @midgetwars1
    @midgetwars1 4 ปีที่แล้ว +2

    Still a bit confused. I think a better example will be needed since I still can't grasp the purpose. The interfaces video was perfect. I understood immediately whey it was useful.

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +1

      Delegates are hard to grasp, especially since we don't often create them until we are creating complex code. The easiest form of delegate to understand (and the most common) is Events. I have a video on them. Check it out. Maybe that explanation will help clear things up.

    • @phimchanel9235
      @phimchanel9235 4 ปีที่แล้ว

      Me either! I'll watch this video for more times.

    • @trustingod0
      @trustingod0 ปีที่แล้ว

      ​@@IAmTimCorey I have never ran into anything in development that was hard to grasp if explained correctly.

  • @nimamirzaalikhan
    @nimamirzaalikhan 3 ปีที่แล้ว

    Best Teacher Out There 🙏🏻

  • @ddishuman
    @ddishuman 5 ปีที่แล้ว

    thanks so much, Tim.
    Majestic!

  • @NilsÅkesson-z9u
    @NilsÅkesson-z9u 11 หลายเดือนก่อน +1

    Was there ever made a video on delegates in asyncronous programming?

  • @AllenMichaelsVlogs
    @AllenMichaelsVlogs 4 ปีที่แล้ว +2

    Very well explained. However I would add that you are not passing the method itself. You are passing a pointer or a pipeline to the method.

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +1

      Yep.

    • @danielhadad4911
      @danielhadad4911 4 ปีที่แล้ว

      So, is there a way to do the same thing but using explicit pointers in C#? I'm just curious.

  • @stuartmurton7319
    @stuartmurton7319 4 ปีที่แล้ว

    Good vid, clear and easy to understand, thank you

  • @drip888
    @drip888 2 ปีที่แล้ว +2

    what is the difference between an action delegate and a normal delegate?

    • @maxron6514
      @maxron6514 2 ปีที่แล้ว +3

      „Action MyDelegate“
      is just a predefined shortcut of
      „public delegate VOID MyDelegate“
      Notice that both don’t return anything. You actually want to avoid a returntype of void (ha ha...), except for events, and use Func instead, which always returns something. There is also a Predicate for Boolean expressions.
      I recommend to read the official exam ref c# book by Microsoft, it explains all of that, and much more about the language itself, very clear on about 450 pages.

  • @PinkyParu
    @PinkyParu 4 ปีที่แล้ว +1

    Wonderful video as usual. I have a question. I have seen in some tutorials using delegate to attach more than one method in it. what is the real time use of it?

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว

      I am not sure what you mean. What are they doing?

    • @PinkyParu
      @PinkyParu 4 ปีที่แล้ว

      @@IAmTimCorey
      public delegate void MyDelegate();
      private void Method1()
      {
      Console.WriteLine("Method1 invoked");
      }
      private void Method2()
      {
      Console.WriteLine("Method2 invoked");
      }
      private void button_Click(object sender, EventArgs e)
      {
      MyDelegate myptr = null;
      myptr += this.Method1;
      myptr += this.Method1;
      myptr.Invoke();
      }
      What is the use of this kind of assignment in real programming?

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +1

      If you wanted to have 0 or more methods fired when something happened. For instance, this is how events work. You might have multiple methods being fired when an event happens.

    • @PinkyParu
      @PinkyParu 4 ปีที่แล้ว

      @@IAmTimCorey Noted. Thank you.

  • @christianrazvan
    @christianrazvan 2 ปีที่แล้ว +9

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

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +10

      That's an...interesting take, especially on a video about delegates.

    • @christianrazvan
      @christianrazvan 2 ปีที่แล้ว +2

      @@IAmTimCorey By the way good job on explaining things 👍
      My comment was refering strictly to OO programming not you

  • @michaelblair5146
    @michaelblair5146 3 ปีที่แล้ว

    What i needed was delegates with events.

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว +1

      Events are delegates. I do have a video on events, if that would help.

  • @skanyer
    @skanyer 6 ปีที่แล้ว

    Will we be seeing more videos? Also, do you ever put methods in the models of the library to run the SqlConnector commands? It seems like it would make sense to have get and save methods on the library classes.

    • @IAmTimCorey
      @IAmTimCorey  6 ปีที่แล้ว +2

      I'm going to be putting out new videos soon. I've had some health issues (I'll be doing a video on this too) that have taken a lot out of me. I'm starting to recover now and I can't wait to start getting videos on here again.
      As for your other question, I'm not sure I'm visualizing what you want to do here. Are you saying to put a get and save method on the model itself? The problem with that would be if you ever wanted to change database types. You would need to change every model.

  • @getright20
    @getright20 4 ปีที่แล้ว +2

    link to the source code is dead....

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +4

      I updated it. It should work now.

  • @vandeljasonstrypper6734
    @vandeljasonstrypper6734 4 ปีที่แล้ว +2

    Tim can you do a video for RealTime library SignalR ?

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +3

      I will add it to the list. Thanks for the suggestion.

    • @vandeljasonstrypper6734
      @vandeljasonstrypper6734 4 ปีที่แล้ว

      @@IAmTimCorey worth the waiting
      You video always have good example show all the features of the library
      Sadly some of the video still in dotnet framework, hope you have time update them to dotnet core. Like dependency injection

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +1

      For the most part, it doesn't matter that it is .NET Framework vs .NET Core for my videos. DI still works the same (maybe slightly different where you set it up but the same in how you set it up) in .NET Core. I will try to cover some differences in various topics, though.

  • @andywalter7426
    @andywalter7426 4 ปีที่แล้ว

    Also, i could not find the video that showed delegates with async. Is there a special video that shows how to do delegates with async. Because that is actually very common. For example, with the example you showed with windows forms. if you used the same example with xamarin forms, then you have to do await because their messagebox requires await otherwise, it runs the rest of the code even though a person did not click the messagebox which causes all kinds of problems. Its also possible that background tasks has to do delegates that need to be thread safe as well.

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว

      I will add it to the list. Thanks for the suggestion.

  • @RobertoLion
    @RobertoLion 2 ปีที่แล้ว

    Sorry I don't understand... is there a redundant repetition at 50:00 ? (Edit: Yes I think there is...)

  • @hakami1426
    @hakami1426 2 ปีที่แล้ว

    17:39 bookmark where it clicked. I can use a delegate to close out nested winforms, which is what i was trying to do

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว

      I am glad you found what you needed.

  • @PerryCodes
    @PerryCodes 2 ปีที่แล้ว +2

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

  • @ryanye6938
    @ryanye6938 3 ปีที่แล้ว

    dosomething( param => callback(param)) is this what delegates seems like?

  • @alistairhalpern8382
    @alistairhalpern8382 5 ปีที่แล้ว

    Why did you put the delegate inside the class rather than the more general namespace? Can a delegate be accessed from anywhere in a solution meaning it has unlimited scope. Did you need to declare references from the UI project to the demo library?

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +2

      (I'll share what happens practically - the theory is a bit more complex) A delegate shares an access point to the code you point at so that the method you send it to can call the code you send it. You don't need to give the caller access to the namespace or library where the code it is calling lives. Remember an event is a type of delegate. For events, we tell it which method to call internally even if the event is fired in an external class. The reason this works is because we basically pass a message (with the associated data) to every indicated location. At that location, the correct method gets called.

  • @Revealed_Potential
    @Revealed_Potential 2 ปีที่แล้ว +9

    This is super hard to understand

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +4

      They are an advanced topic, for sure. It helps to practice them. Seeing them work is sometimes easier than trying to abstractly understand them.

    • @steveo600rr
      @steveo600rr 2 ปีที่แล้ว

      I recommend reading the doc about Delegates and doing some basic examples that MS has in their docs. Then come back and watch the video, it will make more sense.

    • @guidoglielmi7992
      @guidoglielmi7992 2 ปีที่แล้ว +1

      It's funny how as a Javascript user, this is one of the few actually not-so-hard topics for me, considering this is pretty similar to callbacks.

  • @HedgePork
    @HedgePork 4 ปีที่แล้ว

    Hi Tim - is there a problem with the code in the download? When I open the DelegatesDemo.sln (for DelegatesDemo-Start), it does not match the code are showing at the beginning. For example, the GenerateTotal() method only has a single line which throws a not implemented exception. The Main method doesn’t have the line which writes out the total (immediately after the PopulateCartWithDemoData call.) Am I looking at the wrong code?

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว

      Not sure, sorry.

    • @jbassman6
      @jbassman6 4 ปีที่แล้ว

      Yep, it is missing but you can type it out as it is on the video...

  • @VinuP2023
    @VinuP2023 5 ปีที่แล้ว

    Tim, syntax for creating an instance of a delegate is same as that for class, so I'm curious to know is delegate really a class behind the scenes?

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +1

      I don't know the deep underlying structure. I don't believe so, but I'm not totally sure.

    • @VinuP2023
      @VinuP2023 5 ปีที่แล้ว

      Tim,That is ok

    • @BahawalTV
      @BahawalTV 5 ปีที่แล้ว +1

      There is a delegate class. But you can't instantiate it or extend it. It can only be used through DELEGATE keyword.

    • @jlitodelcid326
      @jlitodelcid326 5 ปีที่แล้ว +1

      Not exactly, delegates are more like reference type 'variables'

    • @VinuP2023
      @VinuP2023 5 ปีที่แล้ว

      @@jlitodelcid326 but class is also a reference type right?

  • @StefanKo1224
    @StefanKo1224 4 ปีที่แล้ว

    I understanad everything about delegates but when i want to use it on simple project on wfa with 2 forms i dont know how to do that.Its not hard to understand but it is hard to implement it

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +1

      My recommendation is to start by building a few practice apps that use delegates. Don't make them "do" anything. Just have them use delegates so you get more comfortable with how they work and how to implement them in a fresh application.

  • @larrypeteet5575
    @larrypeteet5575 4 ปีที่แล้ว

    Hi Tim! I lost my links where I saved your content at my Company Favorites. Don't have them anymore. I worked on "CMDemo" and "WPFDemo" which I believe were from you. If so can you point me towards these videos? Both are for WPF

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +1

      I think those were the first and third videos in this playlist: th-cam.com/play/PLLWMQd6PeGY3QEHCmCWaUKNhmFFdIDxE8.html

    • @larrypeteet5575
      @larrypeteet5575 4 ปีที่แล้ว

      @@IAmTimCorey Thanks Again!!

  • @al-omdaahmed3191
    @al-omdaahmed3191 5 ปีที่แล้ว

    thanks a lot bro
    its a great tutorial helped me a lot
    wish you all the luck
    big fan from Gaza Palestine

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +1

      I'm glad it was so helpful.

    • @al-omdaahmed3191
      @al-omdaahmed3191 5 ปีที่แล้ว

      @@IAmTimCorey
      i wonder if you cover MVC request life cycle in one of your videos

  • @Igbon5
    @Igbon5 4 หลายเดือนก่อน

    I just started watching to find out about delegates but the assumed knowledge of LINQ and Lamda expressions puts it beyond me. Maybe I'll be back after learning about those.

    • @IAmTimCorey
      @IAmTimCorey  4 หลายเดือนก่อน +1

      I believe you only need to know about lambda expressions, not LINQ too.

    • @Igbon5
      @Igbon5 4 หลายเดือนก่อน

      @@IAmTimCorey
      decimal subTotal = Items.Sum(x => x.Price);
      Isn't Sum, in Items.Sum, a LINQ extension to query collections?

    • @IAmTimCorey
      @IAmTimCorey  4 หลายเดือนก่อน

      Ah, yep, I didn't realize I had that in this video. Although that isn't necessary for delegates.

    • @Igbon5
      @Igbon5 4 หลายเดือนก่อน +1

      @@IAmTimCorey
      Thanks.
      I am trying to fast track a simple understanding of these things so I can look closer at delegates. I'll be back.

  • @khdvid801
    @khdvid801 6 ปีที่แล้ว

    Thank you sir

  • @leolen1782
    @leolen1782 2 ปีที่แล้ว

    thank youu, helped a lot

  • @paulofernandoee
    @paulofernandoee 2 ปีที่แล้ว

    Good tutorial.

  • @SuperLadly
    @SuperLadly 2 ปีที่แล้ว

    Thanks alot.

  • @rya076
    @rya076 ปีที่แล้ว

    Thank ou so much, I now have better delegates understand.

    • @IAmTimCorey
      @IAmTimCorey  ปีที่แล้ว

      I am glad it was helpful.

  • @michaelblair5146
    @michaelblair5146 3 ปีที่แล้ว +1

    This is incoherent to me (complete beginner to delegates).
    I keep hearing you refer to delegates as methods and i dont understand why when they are parameters passed in a method as a delegate type.

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      That's correct. Delegates are methods that are passed around like parameters so that the method you pass them into can call the passed in delegate whenever they want.

    • @michaelblair5146
      @michaelblair5146 3 ปีที่แล้ว +1

      @@IAmTimCorey This helped me alot. I figured it out i belive. update: nevermind, i didn't learn a thing.

    • @DeeJayh
      @DeeJayh 2 ปีที่แล้ว

      ​ @Michael Blair If I understand what I've learned from this video, the concept of delegates is simple, merely the setup is intimidating. The primary idea is displayed in this video if you ignore the other fluff. Once you understand the primary idea, the fluff starts to make more sense. The primary idea is that you are passing methods from one library/program to another library/program. This makes it so that your library, for example, doesn't need to understand what the Console class is, or what it's method WriteLine() does. It can simply delegate (the word delegate, where the second half sounds like the word "gate") that information back to the passed method and let the method perform the instructions that it needs since it already knows what the Console class is and it's methods like WriteLine(). Rather than having 100 using statements and every program/library you ever write needing to understand every intricate detail of everything it interacts with, it can merely delegate that information back to the callee and let them handle it instead.
      As ​@IAmTimCorey said, think of the events that are triggered by your mouse and keyboard when you design WinForms/WPF applications with a graphical UI... That textBoxDemo_Click method Tim wrote would be pretty useless since he never calls that method anywhere in his code, but because it is linked up with a System.EventHandler when the Button.Click event is called, now it knows to look inside his code for a method with a matching signature. It finds it, and calls it, every time the button's click method is called.
      Tim, while this video is a wealth of information, I have to admit, even I am struggling with the concepts in this video. I absolutely think, if you're up to the unfortunate amount of work that comes with it, that you should re-do this video, and start the approach by teaching that primary concept first (how delegates work) using the button events example. I feel like the rest of the content would be easier to digest if the primary concept was taught first. Even if I perfectly took on the information taught, to the best of my ability, I would have to watch this video twice so that the end would make the beginning easier to digest. (I'll probably have to watch this video 5 times, personally, but that is not your fault in any way.)

  • @ZoidbergForPresident
    @ZoidbergForPresident 6 ปีที่แล้ว +1

    It's maybe because I'm tired but at: (products, subTotal) => { ... } Where do products and subTotal variable come from?
    YEah, I kinda got lost towards the end. :/

    • @IAmTimCorey
      @IAmTimCorey  6 ปีที่แล้ว

      Those are the names I gave the two parameters. We know the method will have two input parameters (because of the declared signature of the method) so when I create the inline method, I shortcut it by just giving the parameter names because C# knows the rest already. Yep, it is a bit confusing until you think through the fact of what C# already knows. Essentially we are letting C# assume the rest because that is what it has to be. The actual names don't matter to the signature, but they do to the implementation (which is what I'm creating here) so I tell the system what the names will be.

    • @ZoidbergForPresident
      @ZoidbergForPresident 6 ปีที่แล้ว

      Oh ok, thanks.

    • @DaveLudwig
      @DaveLudwig 4 ปีที่แล้ว

      @@ZoidbergForPresident this was my breaking point as well. I stared at those inline delegates for a while. But I get it now.

  • @dreamether7808
    @dreamether7808 4 ปีที่แล้ว

    Could someone explain the syntax: {subtotal:C2}
    subtotal is obviously referring to the value passed in...but what the heck is C2 and where is it coming from?

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +1

      C2 is a standard formatting command that tells the system to format the number as currency with two decimal points: docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

    • @muratcoskun4498
      @muratcoskun4498 4 ปีที่แล้ว

      C2 is decimal formatting.
      You can check out the link find more info
      social.msdn.microsoft.com/Forums/vstudio/en-US/9f3a11e5-9456-42c0-8437-348e157ba959/c-format-a-decimal?forum=netfxbcl

    • @muratcoskun4498
      @muratcoskun4498 4 ปีที่แล้ว

      docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings?redirectedfrom=MSDN

  • @ComandanteJ
    @ComandanteJ 4 ปีที่แล้ว +2

    These things can be oh so easily missused. Please, only use these when they make sense and are really the best way to do things. Otherwise, the poor sucker who comes after you will spend an evening wondering what you where trying to do...

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว +2

      With great power comes great responsibility.

    • @glute
      @glute 4 ปีที่แล้ว +2

      Ohhh sooo

  • @RobertoLion
    @RobertoLion 2 ปีที่แล้ว

    What is "message" at 45:26 plz?

    • @RobertoLion
      @RobertoLion 2 ปีที่แล้ว

      I think it's mentioned because it's from instance "cart" of class: "ShoppingCartModel" where "cart" instance is in the beginning before the dot?

    • @akitoakito
      @akitoakito 2 ปีที่แล้ว

      Message is the parameter for the lambda expression that he is using.
      (MyVar) => Console.Writeline($"Cart 2 Alert {MyVar}")

  • @fansban7292
    @fansban7292 5 ปีที่แล้ว

    SIir Why we Use Delegates Why The are required?

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +1

      The video talks you through some options of when they are useful. I can't tell you every case, since there are so many possibilities.

    • @fansban7292
      @fansban7292 5 ปีที่แล้ว

      @@IAmTimCorey Sir Plz Tell Some Comman Advantages

    • @williamgingras304
      @williamgingras304 5 ปีที่แล้ว

      @@fansban7292 In the first example, you can pass a private function of an object to another class and "access" it, instead of making it public

    • @fansban7292
      @fansban7292 5 ปีที่แล้ว

      @@williamgingras304 Thanks

  • @baltarifcan
    @baltarifcan 3 ปีที่แล้ว +16

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

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว +1

      lol

    • @MarcinKralka
      @MarcinKralka 3 ปีที่แล้ว

      You should see the book called "C# 9.0 in a Nutshell" then :) It's > 1000 pages long.

  • @CasshernSinz1613
    @CasshernSinz1613 3 ปีที่แล้ว +12

    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.

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว +4

      Glad it was helpful!

    • @roydonk2878
      @roydonk2878 3 ปีที่แล้ว +5

      Agreed, that's the problem with 99% of tutorials about this. They always approach it from the purely technical side of things, basically just "it can do this and this and this", which just makes it confusing and annoying. Only when seeing it implemented and applied to an actual problem a developer may face does it actually "click" and become relevant.

    • @CasshernSinz1613
      @CasshernSinz1613 3 ปีที่แล้ว +6

      @@roydonk2878 very true, though, that's the problem with most tutorials about any abstract subject.
      Oddly enough I originally went to college to be a researcher in physics and that sort of issue happens constantly when learning math. "This is what this equations does. Memorize it. Look, *writes out equation and solves it in minutes* its so easy."
      I really appreciate videos like this and the people who make them because it has become apparent that teaching (like actual teaching) is a skill not everyone can learn. There are lots of "teachers" in the world and yet so few who are actually able to teach. I'm thankful Tim is someone who can actually teach a subject instead of just reading the definition out load.

  • @titanicpat1275
    @titanicpat1275 2 ปีที่แล้ว +7

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +1

      That is where practice helps. Trying out what you are learning can reveal what you missed or what was not taught. Then you know the right questions to ask to go deeper.

  • @Ravroid
    @Ravroid 3 ปีที่แล้ว +5

    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.

  • @das_evoli
    @das_evoli 4 ปีที่แล้ว +4

    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.

    • @IAmTimCorey
      @IAmTimCorey  4 ปีที่แล้ว

      Practice them for a while. That will help them stick. You don't need to create "real" projects to test what you learn. Just simple little test cases.

  • @mmuneebajaz
    @mmuneebajaz 6 ปีที่แล้ว +4

    Sir have you covered Entity Framework in your videos?

    • @jayjayjaythebrand
      @jayjayjaythebrand 6 ปีที่แล้ว

      COMMANDER he is in his next course coming soon

    • @IAmTimCorey
      @IAmTimCorey  6 ปีที่แล้ว +2

      I cover it only in regards to ASP.Net authentication. Here is why I won't do it more: iamtimcorey.com/ask-tim-dont-use-entity-framework/

  • @faizalvasaya2998
    @faizalvasaya2998 5 ปีที่แล้ว +4

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

  • @bobthebuilder609
    @bobthebuilder609 2 ปีที่แล้ว +3

    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.

    • @jujment699
      @jujment699 2 ปีที่แล้ว +1

      He still encompassed the power of delegates and their use if you watch to the end. He mentioned "this isn't how you'd be using delegates" as a preface to saying he was over using them to show the different types. Stop relying on him and use your programmer mind. How would you condense it into 1? Its easy, all those delegates can actually be made into one single function and delegate. You can do the whole calculation, and message alerts, using one method, and pass that single method to a delegate that returns what you need. He just extrapolated it into the different types for educative purposes.
      It doesn't mean it isn't the proper way to use delegates. This is still the proper way. If you watched the WinUI section of the video, you will see how convenient those delegates ended up being. You can run code in classes completely unrelated and have it compile and work.

  • @BahawalTV
    @BahawalTV 5 ปีที่แล้ว +3

    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.

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +3

      I’m confused. Are you saying my video did not explain that or that other videos do not explain that but I did?

    • @danm4632
      @danm4632 5 ปีที่แล้ว

      @@IAmTimCorey idk what this commenter's point is either. Most videos over explain mostly on why you should use certain concepts but they fail to show actual coding examples. I like that about your videos. I don't fully understand concepts until I see their application. I appreciate your videos. Also caught you on .NET Rocks

  • @pierreplourde
    @pierreplourde 2 ปีที่แล้ว +4

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

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +3

      That's fair: stackoverflow.com/questions/290819/are-delegates-and-callbacks-the-same-or-similar

    • @Voodookillers
      @Voodookillers 2 ปีที่แล้ว

      @@IAmTimCorey or C++ pointers, right?

  • @brunomatzinger3637
    @brunomatzinger3637 ปีที่แล้ว +4

    I love the way you explain things. Delegates have been a closed book for me until now. Now I get it.

  • @tranvanxuat8418
    @tranvanxuat8418 4 ปีที่แล้ว +5

    thank you I am from viet nam

  • @raphaelpinel2614
    @raphaelpinel2614 4 ปีที่แล้ว +4

    Thank you! Great tutorial. Clear explanations :-)

  • @miladabdi3191
    @miladabdi3191 2 ปีที่แล้ว +3

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

  • @2005bgva
    @2005bgva 2 ปีที่แล้ว +2

    Hi Tim, thanks for this video, I would like to ask you about what is the video where you talk how to use delegates in a async way?

  • @Mustafamutasim
    @Mustafamutasim 6 หลายเดือนก่อน +4

    Thanks! You are the best 🌹

    • @IAmTimCorey
      @IAmTimCorey  6 หลายเดือนก่อน +1

      Thank you!

  • @AnandMishra-xs1lt
    @AnandMishra-xs1lt 5 ปีที่แล้ว +3

    believe me i am a beginner , and this video is so confusing

    • @IAmTimCorey
      @IAmTimCorey  5 ปีที่แล้ว +4

      This isn't a beginner video. It is a video for learning delegates for the first time but delegates are an advanced topic. If you want to learn C# in order, check out my Foundation in C# course series at www.iamtimcorey.com/p/complete-foundation-in-c-course-series It will start you from the beginning and take you through to a place where you could be a junior or even mid-level C# developer.

  • @Wildxmo
    @Wildxmo ปีที่แล้ว +2

    I just realized that there was an editing mistake.
    The video has near-duplicate sections at 49:58 and at 50:23. Threw me off there for a second.
    The first duplicate already includes the method "PrintOutDiscountAlert".
    The second duplicate the "PrintOutDuplicateAlert" is added.

  • @deepakha9759
    @deepakha9759 2 ปีที่แล้ว +2

    Hi Tim, Excellent video as always! Got a clear picture of delegates. Just one question. When I try to print currency like you did here in video (C2), it's not displaying in the console. Instead, question mark (?) is printing like below. Any suggestions?
    Example oonsole output --> The subTotal for items in cart 1 is ? 45.42

    • @jannickbreunis
      @jannickbreunis 2 ปีที่แล้ว +1

      Is the culture known in your project? Depening on the culture it could be a $-mark or a €-mark. Maybe when the culture is unknown, ? is displayed?

  • @sent4dc
    @sent4dc 4 ปีที่แล้ว +3

    Anyone coming to this from C++ -- delegates = function pointers.

  • @sinintro4316
    @sinintro4316 3 ปีที่แล้ว +2

    My brains is exploiting, I understood but I still don’t know how to apply it in my applications, I mean I can change one method in my application but I can also change them all so what’s the limit

    • @thfreakinacage
      @thfreakinacage 3 ปีที่แล้ว

      I can give you an example of one place we use Func in our application where I work now. Maybe that will help a little.
      We have a "CacheService" which stores values against a key. The service has one method called "GetOrCreateValue" which takes in a string, which is the key, and a Func. Since we use the cache service in many places in the application, the logic of how we create the value is different everywhere, so that logic can't be added to the cache service. All the cache service does is check in the cache for a value with that key, if it finds it, it returns it. If it doesn't find it then it will run whatever the Func is, then store the value for next time.
      I can try to explain a little better if this was confusing but I'm hoping a real world example might be useful :)

  • @kellybmackenzie
    @kellybmackenzie 2 ปีที่แล้ว +3

    Your channel is awesome, this helped me so much!! Thank you so much!!

  • @trustingod0
    @trustingod0 ปีที่แล้ว +3

    I think this video was helpful as the title states for a practical demonstration, but not sure if the concepts were explained well enough for someone that is watching that doesn't have a lot of experience using delegates. Although, I do understand that we use Func and Action Delegates all the time without having a sound understanding of what delegates are. Just wondering why you didn't mention anything about Callbacks? Sorry if I was to critical, but I am just trying to get a good understanding of Delegates so I can exploit their full potential. Thanks !!!

    • @IAmTimCorey
      @IAmTimCorey  ปีที่แล้ว +4

      A callback is a delegate. It is just another word for it. My recommendation would be to practice what I showed you in the video. That will help give you a firmer grasp on the subject. The problem with delegates/callbacks/actions/events/etc. is that they are a bit complex and there are tons of fancy names being thrown around that makes matters worse instead of better. My remedy is always to get your hands dirty and see what things actually do for yourself. That way, you can have a frame of reference when you are reading confusing terms. I hope that helps. And no, I didn't read that as critical, but thanks for clarifying. I'm glad you are trying to get a good handle on the topic.

    • @trustingod0
      @trustingod0 ปีที่แล้ว +2

      @@IAmTimCorey Thanks Mr. Corey for that excellent advice. I got some help from someone that understands Delegates well and they have me up to speed. They said they commonly use them to notify the user that some event has occurred or after a process has completed asynchronously. I am looking for other use cases to apply them as well, actually I want to use them in a scenario where I am using an Interface, I'm thinking the ultimate goal is to exploit both the power of interfaces and delegates together. Thanks !!!