0

First of all, sorry for bad title as I don't know many correct terms in C#. I found working 'self-unsubscribing' delegate from here. This code below works (at least for me).

private delegate void V;
private V delVoid;

private void SelfUnsubscribeDelVoid()
{
    void Unsubscriber()
    {
        // Some operation
        delVoid -= Unsubscriber;
    }

    DelVoid += Unsubscriber;
}

However, as I use many 'self-unsubscribing' delegates, there will be a lot variation of SelfUnsubscribeDelVoid(), each handle different delegates. Therefore, I make one static function with delegate as input parameter. This way, I don't have to make many similar functions. Here's what I come up with.

public static class Delegates
{
    public delegate void V();

    public static void SelfUnsubscribeDelVoid(V theDelegate, V functionToExecute)
    {
        void Unsubscriber()
        {
            functionToExecute();
            theDelegate -= Unsubscriber;
        }

        theDelegate += Unsubscriber;
    }

}

public class Caller
{
    private Delegates.V OnDoingSomething;
    
    public void FunctionA()
    {
        Delegates.SelfUnsubscribeDelVoid(OnDoingSomething, FunctionB);
        OnDoingSomething();     // FunctionB is not invoked
    }

    private void FunctionB()
    {
        // do something
    }
}

Unfortunately, my code doesn't work. A bit of try and error raise a likelihood that the onDoingSomething delegate is not 'linked' with the input theDelegate, as if they are two different delegates. Are there mistakes in my code? Is my approach incorrect? Or is what I want simply impossible to do? Thanks in advance :)

Edit sample code should've followed common naming convention.

Solihin
  • 1
  • 2
  • What are you trying to do, actually? Why must those delegates self-unsubscribe? – Fildor May 25 '23 at 17:37
  • 1
    `TheDelegate += Unsubscriber` is equivalent to `TheDelegate = (V)Delegate.Combine(TheDelegate, Unsubscriber)`. Your question is essentially "why does assigning my local variable `TheDelegate` have no impact on `OnDoingSomething`?". Because `TheDelegate` is a copy of the current value of `OnDoingSomething`, not a reference to it. – Jeremy Lakeman May 26 '23 at 03:44
  • @Fildor I want to make the `FunctionB` to be delegated and the forget about it. Reducing the burden of "where should I put the unsubscriber". – Solihin May 26 '23 at 08:39
  • @JeremyLakeman So, technically, what I want to accomplish is simply impossible to achieve? – Solihin May 26 '23 at 08:40
  • 1
    Well you could use a `ref` parameter in this specific case. (I'd strongly encourage you to follow .NET naming conventions in sample code, btw - it would make the sample much easier to understand.) – Jon Skeet May 26 '23 at 09:10
  • @JonSkeet My bad, I'll fix the naming rule soon. I've tried using `ref` for `TheDelegate`, unfortunately, compiler get error as it is called in local function (`ref` parameter cannot be used in local function). – Solihin May 26 '23 at 11:31
  • Ah yes, I hadn't thought about that aspect. Of course, you could potentially create your own "mutable delegate" class which wraps the actual delegate... it really depends on how you later use this. – Jon Skeet May 26 '23 at 12:45

0 Answers0