I\'m a c++ developer having used signals & slots in c++ which to me seems to be analogous to delegates in c#. I\'ve found myself at a loss in searching for the functiona
Try the following
class Example {
static void foo(int i) {
Console.WriteLine(i);
}
public static void Main() {
Action someCallback = () => foo(5);
someCallback();
}
}
Or for something even closer to the C++ counter part
class Example {
static void foo(int i) {
Console.WriteLine(i);
}
static Action bind(Action action, T value) {
return () => action(value);
}
public static void Main() {
Action someCallback = bind(foo, 5);
someCallback();
}
}
Explanation. What's happening here is that I am creating a new delegate by means of a lambda expression. The lambda is the expression starting with () =>
. In this case it creates a delegate accepting no arguments and producing no value. It is compatible with the type Action
.