Java 8 java.util.function.Consumer<> 的 c# 等价物是什么?
What is the c# equivalent of Java 8 java.util.function.Consumer<>?
在 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); };
}
Consumer<T>
对应Action<T>
,andThen
方法是排序算子。您可以将 andThen
定义为扩展方法,例如
public static Action<T> AndThen<T>(this Action<T> first, Action<T> next)
{
return e => { first(e); next(e); };
}
"Consumer interface represents an operation that accepts a single
input argument and returns no result"
好吧,只要上面引用自 here 的内容是准确的,它大致相当于 C# 中的 Action<T>
委托;
例如这个 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");
}
}
在 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); };
}
Consumer<T>
对应Action<T>
,andThen
方法是排序算子。您可以将 andThen
定义为扩展方法,例如
public static Action<T> AndThen<T>(this Action<T> first, Action<T> next)
{
return e => { first(e); next(e); };
}
"Consumer interface represents an operation that accepts a single input argument and returns no result"
好吧,只要上面引用自 here 的内容是准确的,它大致相当于 C# 中的 Action<T>
委托;
例如这个 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");
}
}