C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#の匿名メソッドとは何ですか?


匿名メソッドは、名前のないメソッドです。これらのメソッドは、コードブロックをデリゲートパラメータとして渡す手法を提供します。

匿名メソッドは、デリゲートキーワードを使用して、デリゲートインスタンスを作成することで宣言されます。

using System;
delegate void Demo(int n);
namespace DelegateAppl {
   class TestDelegate {
      static int num = 50;
      public static void AddNum(int p) {
         num += p;
         Console.WriteLine("Named Method: {0}", num);
      }
      public static void MultNum(int q) {
         num *= q;
         Console.WriteLine("Named Method: {0}", num);
      }
      public static int getNum() {
         return num;
      }
      static void Main(string[] args) {
         //create delegate instances using anonymous method
         Demo d = delegate(int x) {
            Console.WriteLine("Anonymous Method: {0}", x);
         };
         //calling the delegate using the anonymous method
         d(100);
         //instantiating the delegate using the named methods
         d = new Demo(AddNum);
         //calling the delegate using the named methods
         d(5);
         //instantiating the delegate using another named methods
         d = new Demo(MultNum);
         //calling the delegate using the named methods
         d(2);
         Console.ReadKey();
      }
   }
}

出力

Anonymous Method: 100
Named Method: 55
Named Method: 110

以下は匿名の方法です。

Demo d = delegate(int x) {
Console.WriteLine("Anonymous Method: {0}", x);
};

  1. Javaのクラス/静的メソッドとは何ですか?

    クラス/S タティック メソッドは、特定のオブジェクトインスタンスではなく、クラス自体で呼び出されるメソッドです。静的修飾子は、実装がすべてのクラスインスタンスで同じであることを保証します。クラス/静的メソッドはインスタンス化なしで呼び出されます 静的メソッドは、クラスの他の静的メンバーにのみアクセスできることを意味します。 Javaに組み込まれている静的/クラスメソッドには、 Math.random()、System.gc()、Math.sqrt()、Math.random()があります。 など 構文 public class className {  modifier stati

  2. Pythonクラスの静的メソッドとは何ですか?

    すべてのPythonクラスには、インスタンスメソッド、クラスメソッド、静的メソッドの3種類のメソッドがあります。 例 コードを検討する class OurClass:     def method(self):         return 'instance method called', self      @classmethod     def classmethod(cls):         return 'c