【发布时间】:2016-07-26 18:00:57
【问题描述】:
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); };
}
【问题讨论】:
-
这是java代码,我们是c#程序员,这个接口对我们来说并不简单:)这个coser做什么?
-
任何接受一个参数但不返回值的委托类型都是候选类型。
Action<T>就是其中之一。 -
谢谢丹尼斯,但我怎样才能替代接受方法?
-
您在寻找什么?这个问题没有任何意义。有 lambdas、回调、Observables、LINQ 所有这些都可以做任何你使用
Consumer.accept的事情 -
@PanagiotisKanavos
action.Invoke(parameter)与consumer.accept(parameter)相同。他们只是给了他们不同的名字。但我意识到这不是操作的问题。