What is the c# equivalent of Java 8 java.util.function.Consumer<>?
"Consumer interface represents an operation that accepts a single input argument and returns no result"
Well, provided that this quote was taken from here is accurate it’s roughly an equivalent of the Action<T>
delegate in C#;
For example this java code:
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
Consumer<String> c = (x) -> System.out.println(x.toLowerCase());
c.accept("Java2s.com");
}
}
Converted to C# would be:
using System;
public class Main
{
static void Main(string[] args)
{
Action<string> c = (x) => Console.WriteLine(x.ToLower());
c.Invoke("Java2s.com"); // or simply c("Java2s.com");
}
}
Consumer<T>
corresponds to Action<T>
and the andThen
method is a sequencing operator. You can define andThen
as an extension method e.g.
public static Action<T> AndThen<T>(this Action<T> first, Action<T> next)
{
return e => { first(e); next(e); };
}