JeremyBytes - C# Delegates - Part 1: Basics

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 ม.ค. 2025

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

  • @m.a.g.r1858
    @m.a.g.r1858 4 ปีที่แล้ว +1

    there is always something additional and useful in your videos that others don't talk about and books don't show. You're truly an amazing teacher

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

    Excellent, excellent, excellent! Delegate is a fundamental concept in C#, the base for almost everything like events, callback, lambda expr's, Linq, etc.. Thank you for your teaching style, for me, by far the best tutorial found on internet!

  • @zoomedin-sam
    @zoomedin-sam 6 ปีที่แล้ว +1

    Jeremy - You are an awesome teacher! After years of coding in Java, recently I started working with C#, I didn't spend anytime to learn C# since most of the syntax were very intuitive, however the Linq and Lamda was throwing me under the bus since I didn't learn them in Java. With your lessons everything cleared like a mist cleared on a glass.. thank you!! Please keep doing more of this.

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

    The best C# tutorials I've come across so far. Thank you so much!

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

    Best explanation of delegates I’ve ever seen. Many thanks Jeremy!!!

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

    I appreciate the time and energy you put into your videos. Your videos are great.

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

    Great videos, I knew something big was coming out through out the video... finally at 17:28 it was there. This video is not only talking about syntax but a wholistic concept if I may say, thank you. SOLID principles is a bonus !!!

  • @DetectivePoofPoof
    @DetectivePoofPoof 8 ปีที่แล้ว +5

    What a great video! I love that you went beyond just giving the basic information as so many "Beginner Tutorials" do and actually tied it into the higher level stuff of designing your application.
    This definitely deserves a sub and I' m looking forward to watching your other videos as well.
    Please continue doing what your doing, we need more of this in our community's approach to teaching programming.
    ( Actually we probably need more of this in teaching period :) )

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

    Great C# tutorial!
    A very pleasant, compact presentation of the topic. If only I had come across it earlier. Many Thanks!

  • @destination_moon6392
    @destination_moon6392 7 ปีที่แล้ว

    This is the best and most concise explanation about delegates that I've seen. Thank you!

  • @jebwatson
    @jebwatson 8 ปีที่แล้ว

    Well this was enlightening. I can see the usefulness of these already and your point about SOLID design and open-closed really drove the nail home. Thanks for the generous details put into these videos!

  • @Richtea88able
    @Richtea88able 8 ปีที่แล้ว

    Thanks for the great videos, Jeremy!
    For those of you following along in visual studio there's a slight difference between the code you download to the code in the video and it'll throw an exception if you try to run it. In the person list change the start date from parse to
    'StartDate = new DateTime(1975, 10, 17), Rating=6 },'

    • @jeremybytes
      @jeremybytes  8 ปีที่แล้ว

      Yes, the older code that I have posted on my site has the globalization that you came across (when I originally wrote it, I never thought it would make it outside the U.S.). If you get the latest code from the GitHub repository, you'll find the correction that you suggested: github.com/jeremybytes/delegates-and-func
      The affected file: github.com/jeremybytes/delegates-and-func/blob/master/FuncActionDelegates/Person.cs
      Glad you liked the videos. Thanks for helping to fix the code.

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

    thanks, looking forward to seeing the rest of this series.

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

    Thanks for the tutorial and extremely easy to follow explanation. I can't believe you have less than 2000 subscribers. What is wrong with people in this world!! Keep the content coming! :P

    • @jeremybytes
      @jeremybytes  9 ปีที่แล้ว

      +La Flama Blanca I completely agree. What is wrong with people in this world!?! ;)

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

    Very professional! This is for me not a beginner guide. It's very good and informative. Would be nice to see this execute with an unit test approach. Most regards from Sweden

  • @GuerillahFU
    @GuerillahFU 10 ปีที่แล้ว +5

    I enjoy your teaching style. Thanks for the videos.

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

    Thank you for this great video! I always thought of delegates as being too hard to try to learn but I am applying what you taught in a similar application to learn myself and to be honest they weren't as hard as I made them out to be! Again, thanks for the clear explanation :)

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

    Are delegates same as pointer to functions in C /C++?

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

    Thanks to you and Channel 9 for suggesting your channel. however I have to confess delegates are very confusing to me!! :D

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

    I’m still wondering one thing: Is the delegate passed into a ToString method just an example of how delegates in general work or is this a best practice; does it have benefits over having the PersonToString method defined outside of the class? Since the Person object only passes ‘this’ to the formatter, only public methods are available anyway. Why not pass the Person instance to the formatter via the calling client code?

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

      Passing in a formatter is a common pattern. We can see this in DateTime and other types that use an IFormatProvider. So this is another option that we have available. Whether we do this or another approach depends on how we we want to use our objects and how they interact.

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

    Thank you. Hope to see your new tutorials.

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

    Impressive teaching. Smooth flow.

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

    Thanks for sharing you knowledge. It is very help full video. Please share you knowledge for different topic.

  • @null2846
    @null2846 9 ปีที่แล้ว

    So far what I'm understanding is that a Delegate is like a machine. Every machine has specifications, like a class does - specifically, what inputs does this machine need, and what does the machine output. This is your Delegate definition - like a class definition. In a lot of ways, a Delegate is much more simpler than a class, since it only is a method declaration - kind of like a single entry in an Interface file.
    So in a way, a method that accepts your "machine" is saying "I will take a machine that meets the specifications of a PersonFormat machine" - in this case, all PersonFormat machines (or methods) are _guaranteed_ to accept a Person object, and return a String. In a lot of ways, similar to an Interface so far.
    However, you can actually dynamically pass this machine around like an object. Person objects now have an additional ToString method that accepts PersonFormatter machines. Within the code, there's not much to do, besides invoking the method - this makes it so that virtually you could code/create a method/machine that takes in a Person and spits out a String (of course the method should probably do some formatting in there), but this way you could format it any way you like. Send in a PersonFormat method that
    So in general, methods do *not* _explicitly_ have a "type", but if they have the same signature as the Delegate definition (which is a method signature), a specification set by the Delegate, then yes - that method could be passed in like an object (and later, in the calling code, executed if it wanted to) - you do not explicitly say "this method is a PersonFormat method", you say "This is what a PersonFormat method looks like. Now, everyone - you can all use this specification and know that if you decide to use a passed-in PersonFormat method, it will always accept a Person and return a String - we can now guarantee that. Now, lastly, we are just waiting for code to invoke these client methods (and also send over a method that conforms to the PersonFormat specification as specified in the Delegate definition) that accept a PersonFormat method. Since these client methods accept a PersonFormat method, they'll probably invoke the method and have the data they need to do so (the input/arguments).
    *I know this was a long rant but this stuff is really abstract for me so it helped to write it down in a way that I am trying to understand, hopefully this helped someone. I personally will always see Delegates as a "specification" that a method could meet, and any objects that are of the Delegate's type can hold a reference to any method that meets that specification*.

    • @null2846
      @null2846 9 ปีที่แล้ว

      ... and those objects can be instructed to "invoke" whatever method they are referencing. Like a dynamic GoTo, but with rules.

    • @null2846
      @null2846 9 ปีที่แล้ว

      ... although goto's are hard coded and not dynamic at all

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

    Love your stuff, Jeremy. Fantastic tutorial. Such a breath of fresh air. Ignorant question: Why is your ToString(PersonFormat format) in the Person class not an override? Is it because its parameter of PersonFormat differentiates it from the parameterless ToString() inherited from class Object?

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

      This is a good question. We use "override" for methods with the same parameters. The "ToString()" method in the Person class (with no parameters) is an override that replaces the "ToString()" method in the base Object class. When we add a method with different parameters, such as ToString(Person Format format), this is an overload. This lets us have two ToString methods side-by-side. Which one is used is based on the parameters of the calling code. So we can think of it this way: "override" replaces a method, overloading lets us have methods side-by-side.

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

      Thank you kindly for your reply and explanation. Please keep doing what you're doing. You (your content and how your present it) are very much appreciated!

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

    Hi Jeremy! In all my years of developer this is the best video I have seen for Delegates on C# Thank you very much! I would like to request your permission to do a video based on your content but in Spanish (my first language), Could be possible? I respect your decision. Have a great day!

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

      Hi Fredy! Yes, you can make a video based on this content. I would like this to be helpful to as many developers as possible. I just request that you include a link back to my original video or to my website: www.jeremybtes.com
      Thank you for taking the time to let me know that this video was helpful for you.

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

      Of course! @@jeremybytes Your credits will be there for sure! I feel honored with your kind response :D I started a community in 2008 at my university and it stills active. I am a top user for StackOverflow Spanish version. I enjoy to help to develop our developers community! Thanks a lot!

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

    A delegate is a function pointer.

  • @alejandrov444
    @alejandrov444 9 ปีที่แล้ว

    The first delegates tutorial I can ACTUALLY understand. Thanks a lot. btw a question:
    when you do formatPerson = OutputLastName, you are assigning a method to the delegate type similarly like you could do in javascript in the sense of assigning a method to a variable? if it's the case, In what other situation in c# this technique is used?

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

      +Alejandro Veltri I'm glad this was helpful. Yes, this is assigning a method to a variable (the type of the variable is the delegate type). It's common to use a variable when we want to do multi-casting (multiple methods assigned to a single variable -- see Part 3 of this video series for that).
      We don't often need the intermediate variable, but it's nice to have the option for scenarios that need it. Ultimately, it just gives us different options on how we use delegates in our code.

    • @alejandrov444
      @alejandrov444 9 ปีที่แล้ว

      +Jeremy Clark Great, thanks for your answer. Besides multicasting, is there other common case when you assign a method to a "delegate variable"? or the only situation in c# where you are able to do this is with delegates? (sorry if my questions are a result of a not complete understanding of the topic and/or your answer)

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

      +Alejandro Veltri It's really the same as using other type of variables. For example, we decide whether we want to use a string literal ("Hello") or a variable (greeting). If it's likely to change or we want to reuse the value in multiple places, then a variable makes sense. It's the same with delegates. If we have a single delegate that's unlikely to change, then we can use the method name (or an anonymous delegate). If the delegate is likely to change and/or we want to use the same delegate in different parts of our code, then it might make sense to use a variable. Then we can update the variable (by assigning a different method to it) and the rest of our code stays the same.
      Hope that helps.

    • @alejandrov444
      @alejandrov444 9 ปีที่แล้ว

      +Jeremy Clark It does, thanks for your good and almost immediate answers. If someday you need anything related with Spanish (I'm from Argentina) or some UX concern feel free to contact me, I may be able to help.

  • @CornelionSigismon
    @CornelionSigismon 9 ปีที่แล้ว

    Excellent Video!

  • @ecoldwell
    @ecoldwell 8 ปีที่แล้ว

    Very good tutorial. Thanks!

  • @eyobtse
    @eyobtse 9 ปีที่แล้ว

    That is very good example . Thank you

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

    Really helpful thanks!

  • @abdellah-ia
    @abdellah-ia 8 ปีที่แล้ว

    Great Video, at 1.25 speed. Thanks man

  • @DDStudia
    @DDStudia 9 ปีที่แล้ว

    Good !!!

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

    what the fuck