首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java8 java.util.function.Consumer<>的c#等价物是什么?

Java8 java.util.function.Consumer<>的c#等价物是什么?
EN

Stack Overflow用户
提问于 2016-04-06 19:08:04
回答 1查看 5.8K关注 0票数 10

在C#中是否有与此接口等效的接口?

示例:

代码语言:javascript
复制
Consumer<Byte> consumer = new Consumer<>();
consumer.accept(data[11]);

我已经在Func<>Action<>上搜索过了,但我不知道。

Consumer.accept()接口的原始Java代码非常简单。但对我来说不是:

代码语言:javascript
复制
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); };
}
EN

回答 1

Stack Overflow用户

发布于 2016-04-06 19:47:46

Consumer<T>对应于Action<T>andThen方法是一个排序运算符。您可以将andThen定义为扩展方法,例如

代码语言:javascript
复制
public static Action<T> AndThen<T>(this Action<T> first, Action<T> next)
{
    return e => { first(e); next(e); };
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36449343

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档