在C#中是否有这个接口的等价物?
例如:
Consumer<Byte> consumer = new Consumer<>();
consumer.accept(data[11]);
我搜索了一下 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); };
}
Action<T>
就是其中之一。 - Dennis_EConsumer.accept
的任何操作。 - Panagiotis Kanavospublic static Action AndThen(this Action before, Action after)
{
return t =>
{
before(t);
after(t);
};
}
C# 使用扩展方法来替代默认方法。 - juharraction.Invoke(parameter)
和consumer.accept(parameter)
是同一件事情,只是它们被赋予了不同的名称。但我意识到这不是提问者的问题。 - Dennis_E