java - Java 8 java.util.function.Consumer<> 的 c# 等价物是什么?

标签 java c# consumer

在 C# 中是否有此接口(interface)的等效项?

示例:

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); };
}

最佳答案

"Consumer interface represents an operation that accepts a single input argument and returns no result"

好吧,前提是上面的引述来自 here是准确的,它大致相当于 Action<T>在 C# 中委托(delegate);

例如这个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");
  }
}

转换为 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");
  }
}

关于java - Java 8 java.util.function.Consumer<> 的 c# 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449343/

相关文章:

c# - ImageFailed 与 GridView

c# - 有没有快速的方法来获得鼠标下的控件?

c# - 删除字符串中特定字符后的字符,然后删除子字符串?

java - Kafka 不会均匀地填充主题中的分区

.NET 消费者/生产者(队列)

c# - 如何知道何时停止并行 foreach,其中消费者也是 C# 中的生产者

c# - Java 与 .Net 中的对象生命周期

java - 在java中用正则表达式修改字符串电话号码

java - 为什么我的图像不显示? (Java 图形用户界面)

java - ProGuard:ClassCastException