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

Share
Embed
  • Published on May 28, 2018
  • Blog Post with Code Samples: iamtimcorey.com/delegates/
    Patreon: patreon.com/IAmTimCorey
    Newsletter signup: iamtimcorey.com/general-sign-up
    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 of 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.

Comments • 161

  • DeepSyntax
    DeepSyntax 2 days ago

    I don't think people realize the value of these videos.

  • Guy Provost
    Guy Provost 10 days ago

    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  8 days ago

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

  • Phani Raghavendra Prasad

    Thank U very much! Awesome example and explanation.

  • Russell Kemmit
    Russell Kemmit Month ago +5

    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  Month ago

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

  • Russell Kemmit
    Russell Kemmit Month ago +3

    You are my new best friend!!!!

  • 旺旺
    旺旺 Month ago

    thanks so much, Tim.
    Majestic!

  • Steven K James
    Steven K James Month ago

    cool

  • Alistair Halpern
    Alistair Halpern Month ago

    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  Month ago +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.

  • Frank Thomas
    Frank Thomas 2 months ago

    Hey Tim, do you have a course that discusses this whole application build from start to finish? I've got a fair bit of c# experience, but see that there are a lot of things I can still learn from yourself!

    • Frank Thomas
      Frank Thomas 2 months ago

      @IAmTimCorey Hey Tim, sorry if you do see the previous comment I left here but for some reason I don't see it anymore. Question for you, the big reason I'm interested is because I really want to learn more about delegates/events and also proper thread/task handling in c#. Does this course you mentioned above talk about these topics in depth too?

    • Frank Thomas
      Frank Thomas 2 months ago

      @IAmTimCorey Hey Tim, good to know. I do know a lot about c# in the mvc realm, but I"m interested in learning higher topics like the video content plus better programming using tasks/threads.

    • IAmTimCorey
      IAmTimCorey  2 months ago

      This particular application? No. However, I do have a couple start to finish courses that build an entire application. My C# Application from Start to Finish course does that, plus it has two add-on courses for switching out the WinForm UI with WPF and ASP.NET MVC. I also have a smaller SQLite course that builds a complete application.

  • Al-Omda Ahmed
    Al-Omda Ahmed 2 months ago

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

    • Al-Omda Ahmed
      Al-Omda Ahmed 2 months ago

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

    • IAmTimCorey
      IAmTimCorey  2 months ago +1

      I'm glad it was so helpful.

  • Jorn Van der Wal
    Jorn Van der Wal 2 months ago +1

    15:33 reported sir

    • IAmTimCorey
      IAmTimCorey  2 months ago

      I'm not sure what you mean. Edit: ah, lol.

  • Vandel Jason Strypper
    Vandel Jason Strypper 2 months ago

    I err... confused 😕 should I learn this before or after lambda expression ?
    but still better than other

    • Vandel Jason Strypper
      Vandel Jason Strypper 2 months ago

      @IAmTimCorey Wait hold up
      LINQ ? I thought Lamda was made to replace LINQ in sorting data right ?

    • IAmTimCorey
      IAmTimCorey  2 months ago +3

      This uses lambda expressions so you should know how to use them (at least the basics) first. My Linq 101 video explains how they work: thexvid.com/video/yClSNQdVD7g/video.html

  • COOLBIAN57
    COOLBIAN57 3 months ago +2

    Thanks Tim, I'm a new(ish, lol) computer science bachelor and finding your videos the very best of the best for reviewing these concepts.

    • IAmTimCorey
      IAmTimCorey  3 months ago

      I am glad you are finding these videos helpful.

  • TOABA OMAR
    TOABA OMAR 4 months ago

    great video as always could please you make about reflection the how to use delegate to improve the performance of an app that uses reflection and thanks again for your great videos

    • IAmTimCorey
      IAmTimCorey  4 months ago +2

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

  • comsci
    comsci 4 months ago +1

    dude just get to delegates please. you are teaching us how to debug and all the other stuff. get to the point.

    • Prime v_x
      Prime v_x 3 months ago

      @IAmTimCorey You are right off course about providing context. However, comsci is not wrong either, you go of into these little tiny tangents about format specifiers and tuples. And short as these might be they completely distract me from the core lesson which is Delegates.

    • Maciej K
      Maciej K 4 months ago

      I for one enjoy the additional context. The videos are longer, but more comprehensible.

    • Muhanad Ahmad
      Muhanad Ahmad 4 months ago +2

      Go watch the other videos of 5 min and understand how delegate is a pointer of a function with no actual knowledge.....good luck

    • IAmTimCorey
      IAmTimCorey  4 months ago +13

      The point is that delegates don't live in a vacuum. Learning pieces of coding is useless if you don't understand how and when to use them and how they fit in a bigger picture.

  • K4MR4N1UK123
    K4MR4N1UK123 5 months ago

    Just use the bloody pointers.

  • fans ban
    fans ban 5 months ago

    SIir Why we Use Delegates Why The are required?

    • fans ban
      fans ban 5 months ago

      @William Gingras Thanks

    • William Gingras
      William Gingras 5 months ago

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

    • fans ban
      fans ban 5 months ago

      @IAmTimCorey Sir Plz Tell Some Comman Advantages

    • IAmTimCorey
      IAmTimCorey  5 months ago

      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.

  • fans ban
    fans ban 5 months ago

    Sir i Have a Question What is Difference Between Linq and PLinq

    • fans ban
      fans ban 5 months ago +1

      Thanks Sir I Had a very good Concept about it now after visiting to your Given website linq and By Seeing The Comment of Yours Thanks!

    • IAmTimCorey
      IAmTimCorey  5 months ago

      The "p"? Sorry, easy joke. Plinq is parallel linq, meaning the operations happen in parallel. There are limited uses for it, but when you do have a use for it, it is really helpful. Here is more info: docs.microsoft.com/en-us/dotnet/standard/parallel-programming/introduction-to-plinq

  • Riyaan N
    Riyaan N 5 months ago

    Hi Tim, Thanks for this video. I could see mentionDiscount delegate return type is 'void' and Func CalculateDiscountedTotal return type is 'decimal' , so you have return calculateDiscountedTotal , but what should be done when delegate mentionDiscount also returns something. you cannot have two return statements right?

    • Riyaan N
      Riyaan N 5 months ago

      @IAmTimCorey thanks a lot

    • IAmTimCorey
      IAmTimCorey  5 months ago

      They can both return something because each is a separate call. The resulting method could use both return values to then return an aggregate (if applicable). Also, you can return more than one thing if you would like. It is called a Tuple.

  • Layarion
    Layarion 5 months ago

    Ok watched the whole video. *question* naming conventions for delegates? Would it be fine if I put Delegate at the end of the name of each custom delegate i make?
    just on the surface of things, to me it seems like someone could get dizzy faster than with other things if a delegate has a bad name.

    • IAmTimCorey
      IAmTimCorey  5 months ago

      I don't see any major problems with doing that. It describes what it is and there isn't a time when a delegate isn't a delegate. Go for it.

  • Layarion
    Layarion 5 months ago

    ohhh so maybe this is how I can have a library display information to the user without having a bunch of strings that the end app has to search out to display.

    • Layarion
      Layarion 5 months ago

      @IAmTimCorey i don't know, but i'll look into it thanks. just a noob creating a helper tool for a video game that uses xml modding.

    • IAmTimCorey
      IAmTimCorey  5 months ago

      Can you create your own markdown-style format that you then parse on the other end? Something like "This is normal text. This is green text This is normal text again"

    • Layarion
      Layarion 5 months ago

      @IAmTimCorey well i wish ... you know how \n is a new line? well i wish i could color the text with something similar, because otherwise i have to have the UI do it...which means then i need to keep the segment of text i want colored, in it's own space/variable.

    • IAmTimCorey
      IAmTimCorey  5 months ago

      Isn't it fun when you find a new feature to play with?

  • Antonio
    Antonio 6 months ago

    @IAmTimCorey, Question please. Let's say we have classes: ClassA and ClassB. Basically a Delegate is just a pipeline or "chain" which points to a method / custom method. For example if you declare a delegate in ClassA and there you have a method that takes a delegate as a parameter, you can call that method from ClassB or any location in the application as long as it's visible and pass a parameter that matches the signature of the Delegate in ClassA. Then the logic of the method of ClassA will get executed normally and at some point inside of the method body of ClassA you can call that parameter ( which is a method ), the delegate declared in ClassA will be chained to the passed method and the body of the method which resides in ClassB will be executed. That's why they are assimilated with Function Pointers from C++.

    • Antonio
      Antonio 6 months ago

      @IAmTimCorey - if all the nonsense I've written above is a delegate, more or less :-). At least that's how I understood it - I'm not so good at explanations when I do not fully understand things.

    • IAmTimCorey
      IAmTimCorey  6 months ago +1

      I'm not sure what your question is.

  • Stephen DeCaro
    Stephen DeCaro 6 months ago

    This has been so helpful! Thank you!

  • Michael Eichner
    Michael Eichner 6 months ago

    Great video as usual.
    I have two questions.
    1. I noticed that in the console example methods like SubTotalAlert were static, but in the winforms example they weren’t static. Am I correct that the method being static or not static has nothing to do with the delegates? In other words the console example didn’t need static methods?
    2. Given the following scenario:
    I have several customers and hope to keep adding customers. Most calculate the discount in their shopping carts the same way. A few have their own custom calculations.
    Question: Would I be better off having my Shopping Cart class, use a delegate, an interface or inheritance where the Shopping Cart class is the base class that has a method called CalculateDiscount, and then override that method in customer specific Shopping Cart classes?
    Thanks

    • Michael Eichner
      Michael Eichner 6 months ago +1

      IAmTimCorey Thanks for the fast response and opening up the world of delegates in such a clear manner.

    • IAmTimCorey
      IAmTimCorey  6 months ago +1

      Yes, static has nothing to do with the delegate (how it works). WinForms are in a class instance already. Console apps start from a static class. You can still use class instances in a Console app but I didn't bother.

      That really depends on your scenario but it sounds like an inheritance situation, where you override the method in certain circumstances.

  • mrmcbuggles
    mrmcbuggles 6 months ago

    Hello, Do you have any videos on dynamic expression trees with generics, Expression.Call, etc.? Edit: Should have said, to make dynamic SQL statements beyond the simple .Equal or the like. Things like "Where" and "Contains."

    • mrmcbuggles
      mrmcbuggles 6 months ago

      @IAmTimCorey I understand. FWIW, if you do get further requests, I am speaking about Dynamic SQL based upon Expression Trees in Linq. Maybe Dynamic Linq Expressions is the best way to say it. Since we can't use strings to change the Linq Expression (like in .Where, .Contains, etc.), we have to use Expression Trees. It's easy enough, but it gets complicated when the stock .Equal and such are not what you need and you have to use] a .Where. There is a Dynamic Linq library from way back that Scott Guthrie introduced, but that is not what I am mentioning here. In any case, thanks for the videos.

    • IAmTimCorey
      IAmTimCorey  6 months ago +1

      I do not have a video on dynamic expression trees. I have found those to be used so little as to almost be unnecessary. My philosophy is to not create videos on topics that are very small and probably should stay that way. If I create a video on the topic, I'm almost encouraging people to use it. All that to say that I'll probably not cover this topic in a video (but thanks for asking).

      As for dynamic SQL, it depends on what you mean. LINQ performs SQL-type operations (filtering, sorting, etc.) and I do have a video on that. Creating dynamic SQL statements themselves, though, is not something I've covered.

  • Insomnia
    Insomnia 7 months ago

    Hey Tim! Great video as always. I do have a question which has always kind of haunted me. Since I use WPF and typically have multiple windows; sometimes I need to update the main GUI from say a settings window. A setting could be changed which affects the main GUI and then it needs to update. Would it be better to use a delegate which I guess could probably be passed into the constructor of the window? Or would it be better to fire an event off saying some list has updated or whatever and the main GUI could listen for that?
    This interaction between windows in WPF has always confused me and I've seen people pass an instance of their main viewmodel via "this" and I do that now but it feels janky and just wrong when you go through the entire WPF process and then do some dirty trick like that. I mean idk what is right and have never found a good solution online so what do you think?

    • IAmTimCorey
      IAmTimCorey  7 months ago

      If you are using MVVM in WPF, you would use the event aggregator. If not, and if this is common, you could create your own. Otherwise, passing around a class instance is actually a good idea. It is making use of the benefits of OOP.

  • Pieter W
    Pieter W 7 months ago +1

    Great explanation. Not to fast and taking me step by step. Thanks. Used lamba expressions for a while now but its always nice to know what they do

  • semen083
    semen083 7 months ago +1

    In my opinion tbe best profit of using Func and Action it is use in Expression. The best way of delegation of method implementation it is using Interfaces.

    • IAmTimCorey
      IAmTimCorey  7 months ago

      Well, there are a lot of uses for each. The key is to limit yourself to choosing the best tool for the job, just just the cool one.

  • godfathermikal
    godfathermikal 8 months ago +2

    This video is awesome! Thank you.
    Do you did pluralsight courses?

    • IAmTimCorey
      IAmTimCorey  8 months ago

      I have not done a Pluralsight course (yet - no plans at this point either). I do have my own site ( IAmTimCorey.com ) where I sell courses but I haven't partnered with a company like that to sell my content as part of a subscription.

  • Marta Krzyk
    Marta Krzyk 8 months ago +2

    Awesome video!

  • Sanwal Chaudhry
    Sanwal Chaudhry 8 months ago +2

    awesome explanation... I will recommend this video to every beginner programmer.

  • Wttftyh Gytyf
    Wttftyh Gytyf 9 months ago +2

    Came here for SOLID but checking your versions on other topics too haha.

  • quicktastic
    quicktastic 9 months ago +1

    Although delegates are not that complicated in general. It does seem to be a concept that doesn't lend itself well to explanation. Mainly because of the seemingly disconnected entities being created which leads to a lot of "what did he just do? where did he put that?". You do it about as well as it can be done. Nice video.

  • UltimateYT
    UltimateYT 9 months ago

    1:50

  • Rascovici Bogdan
    Rascovici Bogdan 9 months ago +1

    great explanation ! keep up the good work :D

  • Khuraijam Upendrajit
    Khuraijam Upendrajit 10 months ago +1

    Thank you sir

  • dowdag
    dowdag 11 months ago +2

    Thanks, However still confused, maybe another IAmTimCorey video on just Delegates and Events would be
    helpful to jr. net developers :)

    • IAmTimCorey
      IAmTimCorey  11 months ago +2

      They will be in a start to finish series or two. In the meantime, practice what you see in the videos. It will help, even if you don't understand everything.

  • Huggabearskij
    Huggabearskij Year ago +6

    I'm so incredibly grateful that people like Tim exists that takes the time to show how it actually works so I don't have to waste my life at stack overflows 8 year old outdated answers. Thanks!

  • Gil Cooli
    Gil Cooli Year ago +2

    Hi Tim. I hope you feel better soon. I miss your lessons and wish you a good health.

    • IAmTimCorey
      IAmTimCorey  Year ago +4

      I'm finally getting better. New videos are coming soon.

  • Shawn Kanyer
    Shawn Kanyer Year ago

    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  Year ago +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.

  • Twenty Twenty
    Twenty Twenty Year ago +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.

    • Twenty Twenty
      Twenty Twenty Year ago

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

    • Twenty Twenty
      Twenty Twenty Year ago

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

    • IAmTimCorey
      IAmTimCorey  Year ago

      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.

  • tanu1215
    tanu1215 Year ago

    Thanks for the video, Tim! I'd appreciate some videos on low level stuff. How the compiler works, for example.

    • IAmTimCorey
      IAmTimCorey  Year ago

      That's an interesting idea but it is probably a bit outside of the scope of my channel though.

  • mattd390
    mattd390 Year ago

    Are you ok, Tim??

    • Sireesha E
      Sireesha E Year ago +1

      IAmTimCorey get well soon

    • mattd390
      mattd390 Year ago +1

      IAmTimCorey get well, buddy!

    • IAmTimCorey
      IAmTimCorey  Year ago +5

      I let my mailing list know and I'm planning on doing a small piece on it in my next video (hopefully this Monday) but I'm recovering. I had a heart issue that took me down and has been difficult to recover from. My heart is healthy but a virus is attacking the lining so I'm weak and have to watch what I do. I'm on the road to recovery but it is taking time.

  • offichat
    offichat Year ago

    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  Year ago +1

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

  • Int Er
    Int Er Year ago

    Do you have videos you can make on working with APIs in .NET/C#

    • IAmTimCorey
      IAmTimCorey  Year ago

      Not yet. I do have a video on making an API but not yet with working with them. Soon though.

  • Bright Ndiweni
    Bright Ndiweni Year ago

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

  • Daniel Hosseini
    Daniel Hosseini Year ago

    Hi Tim, is there a way to check if a delegate have been passed in the method? For example I only want to run the delegate method if it’s has been passed in my method signature. Maybe if(delegateMethod !=null) run delegateMethod. Is that possible?

    • Daniel Hosseini
      Daniel Hosseini Year ago +1

      IAmTimCorey works like a charm. thanks Tim, you're the best!

    • IAmTimCorey
      IAmTimCorey  Year ago

      It is possible and it is simle to do. For example, in my tellUserWeAreDiscounting delegate, I just alert the user that we are applying a discount. In my WinForm second example, I passed in an empty method to bypass this call. I could have passed in null. The only problem is that my code would have crashed when it tried to call this line:
      tellUserWeAreDiscounting("We are applying your discount.");
      so instead I could do this:
      tellUserWeAreDiscounting?.Invoke("We are applying your discount.");

      The question mark dot Invoke allows us to stop if the delegate is null but, if not, go ahead and call it.

  • ZoidbergForPresident

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

    • ZoidbergForPresident
      ZoidbergForPresident Year ago

      Oh ok, thanks.

    • IAmTimCorey
      IAmTimCorey  Year ago

      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.

  • Jan Smrčka
    Jan Smrčka Year ago +1

    Well done. You explanation is clear like a diamont :-)

  • Clovis Shropshire

    We've waited for this and the wait was more than worth it. I'm bumping up my Patreon donations

  • CharmedQuarkSystems

    Ultimately you probably could have just addressed the more fundamental concept of a 'framework'. Not sure if that term is used in C#, but this is just an example of thousands of such things that come up in non-trivial applications where you have a generic chunk of logic which can be specialized by passing in callbacks (whatever you want to call them in the particular language, that's what all these things are a way to call back to the code that created you.)
    So it's a substantial piece of code that has various 'sockets' into which you can plug other things that implement known interfaces (method or class) which the main piece of code uses to do things that may need to vary depending on the calling code. This is a common thing in any object oriented language. I use it a lot in C++ (pure virtual mixin interfaces, which are like interfaces in C#) or either static method protocols or class method prototypes.
    UI libraries are often called UI Frameworks because that's exactly what they are doing. The GUI controls (widgets, windows, whatever) are the well defined interfaces that you can create and plug into the large, generic hunk of logic (the windowing system) that lets you customize what it does, without modifying it directly. So it allows you to apply that windowing system logic to endless specific applications.
    Personally I find the interface/mixin thing nicer, since any class can implement it directly and the called back object has access to the members of the target object being called (which wouldn't be the case if you are calling back on a static method.) The next step beyond that would be something highly decoupled like publish/subscribe, where you lose a lot of compile time safety in order to get very loose coupling.

    • IAmTimCorey
      IAmTimCorey  Year ago +1

      Good discussion. I think the key here is the purpose of my video. My goal for any video is clear understanding of how to use a thing in the real world and why. I try for as simple an explanation as possible as part of the way i achieve that goal. Framework didn't seem to fit in this video but probably in a future video.

    • TheMonk72
      TheMonk72 Year ago

      CharmedQuarkSystems I agree that frameworks (in the colloquial sense) are important, but in a video like this they are probably out of scope. Worthy of a mention certainly.

    • CharmedQuarkSystems
      CharmedQuarkSystems Year ago

      Sure, but I just always feel like, if you put forward the overall pattern first, sometimes these things are easier to understand than if you just present them as standalone mechanisms. So many things fall into the categories of callbacks and frameworks, and understanding those basic ideas means that you have a (pardon the pun) framework to place all of the actual mechanisms that might be used to implement those concepts into.
      In the abstract, it's not a hard concept to grasp. And that means you know what's coming, it's just a matter of details.

    • TheMonk72
      TheMonk72 Year ago

      A delegate is actually very simple - it's a typed closure over a function pointer and an object reference. Always the same structure in memory, the type information is used at compile time to determine how it is called.
      The complexity of handling captured variables and so on for Func and Action is handled by the compiler. It will either emit a static method or a struct depending on the requirements of the object then create a closure to the actual compiled method. This way we can use a compatible Func or Action via syntactic sugar.
      Grab a free copy of LINQPad and have a look at the generated IL for a simple example to see how this all works behind the scenes. C#'s compiler does a lot of things like this.

    • CharmedQuarkSystems
      CharmedQuarkSystems Year ago

      BTW, most of that post was not for you. I'm sure you know those things. It was for other folks, who might wonder what the connection is between 'frameworks' and what you were discussing here.

  • Antonio Vitas
    Antonio Vitas Year ago

    50:00 and 50:26
    thexvid.com/video/z_KmNZNT5xw/video.html
    jokes aside, thanks for the video Tim.

  • Harag
    Harag Year ago

    Great video, nice and clear! Looking forward to the Async video / threading video.

  • Fernando Longuini
    Fernando Longuini Year ago +1

    Great video Tim, but I still need to watch a feel times to really understand

  • Husam K
    Husam K Year ago

    JavaScript is therefore very powerful because by its nature can do the same thing here

    • IAmTimCorey
      IAmTimCorey  Year ago +3

      Trying to compare JavaScript to C# isn't really an apt comparison. I love both. JS is really good in the client side browser. C# is really good on the server and in business apps. JS is free form and dangerous. C# is type safe and protected. The languages have a lot of similarities in ability because most major languages do. They key is to pick the right language for the job. If you are going to write an API and do backend work on a website that talks to SQL, you probably should pick C#. If you want to talk to that API and do client side display manipulation, JS is probably your best choice. At my job, I do both for that very reason.

    • danko95bgd
      danko95bgd Year ago

      Trevor Belmont there is async await in javascript now too

    • Peter Sabry
      Peter Sabry Year ago

      JavaScript is a mess

    • Trevor Belmont
      Trevor Belmont Year ago +1

      Husam K async code is easier and saner to read and write in c# tho. C# has async await by default and not worry about callback hell.

  • joaquin acuña
    joaquin acuña Year ago +1

    Thanks Tim to take your time to explain this, is very useful for who is learning C # and for me, of course! Keep on going!

  • Trung Nguyen Quang

    Thanks

  • asyncawake
    asyncawake Year ago +1

    This was high on my wishlist, can't wait to watch!

  • COMMANDER
    COMMANDER Year ago +3

    Sir have you covered Entity Framework in your videos?

    • IAmTimCorey
      IAmTimCorey  Year ago +1

      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/

    • Clovis Shropshire
      Clovis Shropshire Year ago

      COMMANDER he is in his next course coming soon

  • Luigi Zambetti
    Luigi Zambetti Year ago +3

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

    • IAmTimCorey
      IAmTimCorey  Year ago +1

      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.

    • Trevor Belmont
      Trevor Belmont Year ago +2

      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

    • Surya Das
      Surya Das Year ago +2

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

    • Jim Cheseborough
      Jim Cheseborough Year ago +2

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

  • Bruno Kučević
    Bruno Kučević Year ago +3

    Hell yes!! Cant wait to watch it later today. Hope it has func and action covered

    • Bruno Kučević
      Bruno Kučević Year ago

      Next up dictionaries? Right? Hehehe.

    • Bruno Kučević
      Bruno Kučević Year ago

      Its awesome. Still a bit of abstract but awesome. Ill watch it till i understand it.

    • IAmTimCorey
      IAmTimCorey  Year ago +1

      Let me know what you thought of it.

    • Bruno Kučević
      Bruno Kučević Year ago +1

      Excellent. As long as he sticks to his elaboration techniques i will be more than satisfied.

    • joaquin acuña
      joaquin acuña Year ago +1

      yup, he covers that topics too