How to dynamically connect signals with parameters in C# ?

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

They work fine when I use signals without parameters, like this:

button.Pressed += () => Func();
     void Func()

But when signals have parameters, they fail to compile like this(CS1593):

     button.Toggled += () => Func();
     void Func(bool isToggled)

Sorry, maybe the question is simple, but I don’t understand how to do it.

:bust_in_silhouette: Reply From: Adrasteon

With this line of code, you are trying to call a function with a boolean parameter, without specifying parameters:

button.Toggled += () => Func();

You should call Func with a boolean parameter:

button.Toggled += () => Func(true);

Thank you for your help.
After some experimentation I found the right way to write them, they look like this:

button.Toggled += (bool n) => Func(n);

airs1991 | 2023-04-08 23:47