Is there a way to provide additional arguments for Pressed-event in C#?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Raymond

I have buttons generated via a script, and I want a method with parameters to be able to subscribe and unsubscribe to/from those buttons’ “Pressed”-events. Or in other words, how can I send additional arguments with a “Pressed”-event?

Something like this:

    Button.Pressed += DoSomething;

    public void DoSomething(string text)
    {
        // Do something with the text
    }

That code obviously doesn’t work, so how could I implement that kind of behavior in C#?

:bust_in_silhouette: Reply From: juppi

You could use such a Lambda expression:

Button.Pressed += () => DoSomething("Hello from Button!");