Java 的 java/util/function/BiFunction 的 C# 等价物是什么?
What is the C# equivalent of Java's java/util/function/BiFunction?
在 Java 中有一个名为 BiFunction
的接口 this source:
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(apply(t, u));
}
}
并且 oracle document 指出它是这样工作的:
Represents a function that accepts two arguments and produces a
result. This is the two-arity specialization of Function. This is a
functional interface whose functional method is apply(Object, Object).
apply
R apply(T t, U u)
Applies this function to the given arguments. Parameters: t
- the first function argument u
- the second function argument
Returns: the function result
andThen
default <V> BiFunction<T,U,V> andThen(Function<? super R,? extends V> after)
Returns a composed function that first applies this
function to its input, and then applies the after function to the
result. If evaluation of either function throws an exception, it is
relayed to the caller of the composed function. Type Parameters: V
-
the type of output of the after function, and of the composed function
Parameters: after
- the function to apply after this function is
applied Returns: a composed function that first applies this function
and then applies the after function Throws: NullPointerException
- if
after
is null
我想知道如何将此接口实现为 class 或 C#
中的扩展方法,C#
中是否有用于此的 class默认值?
C# 没有像 Java 那样使用函数式接口来表示函数,而是使用 delegates. There is a built in delegate - Func<T1,T2,TResult>
,它表示具有 2 个参数和非空 return 值的函数,就像BiFunction
在 Java.
对于 apply
代表,您可以像 function/method 一样称呼他们。示例:
Func<int, int, int> add = (x, y) => x + y;
// similar to: System.out.println(add.apply(1, 2));
Console.WriteLine(add(1, 2));
如果你真的想在那里写一句话,你可以写Invoke
:
add.Invoke(1, 2);
它没有 andThen
方法,但您可以为此编写一个扩展方法:
public static Func<T1, T2, R> AndThen<T1, T2, U, R>(this Func<T1, T2, U> bifunction, Func<U, R> andThen)
=> (x, y) => andThen(bifunction(x, y));
在 Java 中有一个名为 BiFunction
的接口 this source:
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t, U u) -> after.apply(apply(t, u));
}
}
并且 oracle document 指出它是这样工作的:
Represents a function that accepts two arguments and produces a result. This is the two-arity specialization of Function. This is a functional interface whose functional method is apply(Object, Object).
apply
R apply(T t, U u)
Applies this function to the given arguments. Parameters:t
- the first function argumentu
- the second function argument Returns: the function result
andThen
default <V> BiFunction<T,U,V> andThen(Function<? super R,? extends V> after)
Returns a composed function that first applies this function to its input, and then applies the after function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function. Type Parameters:
V
- the type of output of the after function, and of the composed function Parameters:after
- the function to apply after this function is applied Returns: a composed function that first applies this function and then applies the after function Throws:NullPointerException
- ifafter
is null
我想知道如何将此接口实现为 class 或 C#
中的扩展方法,C#
中是否有用于此的 class默认值?
C# 没有像 Java 那样使用函数式接口来表示函数,而是使用 delegates. There is a built in delegate - Func<T1,T2,TResult>
,它表示具有 2 个参数和非空 return 值的函数,就像BiFunction
在 Java.
对于 apply
代表,您可以像 function/method 一样称呼他们。示例:
Func<int, int, int> add = (x, y) => x + y;
// similar to: System.out.println(add.apply(1, 2));
Console.WriteLine(add(1, 2));
如果你真的想在那里写一句话,你可以写Invoke
:
add.Invoke(1, 2);
它没有 andThen
方法,但您可以为此编写一个扩展方法:
public static Func<T1, T2, R> AndThen<T1, T2, U, R>(this Func<T1, T2, U> bifunction, Func<U, R> andThen)
=> (x, y) => andThen(bifunction(x, y));