在C#中是否有相当于这个接口?
例:
Consumer<Byte> consumer = new Consumer<>();
consumer.accept(data[11]);
Run Code Online (Sandbox Code Playgroud)
我四处搜寻Func<>,Action<>但我不知道.
Consumer.accept()接口的原始Java代码非常简单.但不适合我:
void accept(T t);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
Run Code Online (Sandbox Code Playgroud)
Fab*_*jan 10
"Consumer接口表示接受单个输入参数并且不返回任何结果的操作"
好吧,如果这个引用来自这里是准确的,它大致相当于Action<T>C#中的委托;
例如这个java代码:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
在C#中会是:
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");
}
}
Run Code Online (Sandbox Code Playgroud)