Your current attempt does not work, because you are trying to declare a variable of type void - the equivalent will not work in C # either. You need to declare a variable of the appropriate functional interface, just like you use the delegate type in C #.
You can do this with a lambda expression, but it would be better (IMO) to use a method reference:
import java.util.function.Consumer; public class Test { public static void main(String[] args) { Consumer<Object> c1 = x -> System.out.println(x); Consumer<Object> c2 = System.out::println; c1.accept("Print via lambda"); c2.accept("Print via method reference"); } }
Here, the Consumer<T> interface is generally equivalent to the Action<T> delegate in .NET.
Similarly, you can use method group conversion in C # rather than a lambda expression:
public static Action<object> WL = Console.WriteLine;
Jon skeet
source share