Java 函式式介面在 C# 中的等價形式


Java 函式式介面在 C# 中等效於委託。

下面讓我們瞭解 Java 中函式式介面的實現 −

示例

@FunctionalInterface
public interface MyInterface {
   void invoke();
}
public class Demo {
   void method(){
      MyInterface x = () -> MyFunc ();
      x.invoke();
   }
   void MyFunc() {
   }
}

C# 委託中同樣的實現 −

示例

public delegate void MyInterface ();
public class Demo {
   internal virtual void method() {
      MyInterface x = () => MyFunc ();
      x();
   }
   internal virtual void MyFunc() {
   }
}

更新於: 22-Jun-2020

2 千+ 瀏覽量

開啟您的職業生涯

完成這門課程,獲得認證

開始學習
廣告