Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I like to think of a delegate as "a pointer to a function". This goes back to C days, but the idea still holds. </p> <p>The idea is that you need to be able to invoke a piece of code, but that piece of code you're going to invoke isn't known until runtime. So you use a "delegate" for that purpose. Delegates come in handy for things like event handlers, and such, where you do different things based on different events, for example.</p> <p><a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/using-delegates" rel="nofollow noreferrer">Here's</a> a reference for C# you can look at:</p> <p>In C#, for example, let's say we had a calculation we wanted to do and we wanted to use a different calculation method which we don't know until runtime. So we might have a couple calculation methods like this:</p> <pre><code>public static double CalcTotalMethod1(double amt) { return amt * .014; } public static double CalcTotalMethod2(double amt) { return amt * .056 + 42.43; } </code></pre> <p>We could declare a delegate signature like this:</p> <pre><code>public delegate double calcTotalDelegate(double amt); </code></pre> <p>And then we could declare a method which takes the delegate as a parameter like this:</p> <pre><code>public static double CalcMyTotal(double amt, calcTotalDelegate calcTotal) { return calcTotal(amt); } </code></pre> <p>And we could call the <code>CalcMyTotal</code> method passing in the delegate method we wanted to use.</p> <pre><code>double tot1 = CalcMyTotal(100.34, CalcTotalMethod1); double tot2 = CalcMyTotal(100.34, CalcTotalMethod2); Console.WriteLine(tot1); Console.WriteLine(tot2); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload