2016-04-06 15 views
6

C#にはこのインタフェースと同等の機能がありますか?Java 8のjava.util.function.Consumer <>のC#に相当するものは何ですか?

例:

Consumer<Byte> consumer = new Consumer<>(); 
consumer.accept(data[11]); 
私はのFunc <周りを検索しました

>とアクション<>が、私はわかりません....

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

これは、Javaコードであり、我々はC#のプログラマーであり、このインターフェースは、私たちに簡単ではありません。)WAHTをこのコスマーナは何をしますか? –

+2

1つのパラメータを取り、値を返さないデリゲート型はすべて候補になります。 'Action 'がその一つです。 –

+0

デニスさんありがとうございます。 –

答えて

6

まあhereから取られたこの引用は正確であるならば、それは内Action<T>デリゲートのと同じですC#;

例えば

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#に変換されたこのJavaコードは、私たちに与えるだろう:

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

Consumer<T>Action<T>とに対応していません210メソッドはシーケンシング演算子です。拡張メソッドとしてandThenを定義することができます。

「消費者インタフェースは、単一の 入力引数を受け入れ、結果を返さない操作を表し、」
public static Action<T> AndThen<T>(this Action<T> first, Action<T> next) 
{ 
    return e => { first(e); next(e); }; 
}