ホームページ  >  記事  >  バックエンド開発  >  C# デリゲート

C# デリゲート

WBOY
WBOYオリジナル
2024-09-03 15:29:52452ブラウズ

C# デリゲートは、コード内でイベントやコールバックを処理する場合、または関数に異なるデータ型のパラメーターが複数あるが、パラメーターではなく関数自体を渡したい場合に重要な役割を果たします。 。この場合、デリゲートは関数へのポインターのように機能し、参照データ型であるためメソッドの参照を保持するため、デリゲートが登場します。デリゲートは、C# の System.Delegates クラスの一部です。これらは、C および C++ プログラミングの関数ポインターに似ています。

構文

C# でデリゲートを宣言する構文を見てみましょう

<access modifier> delegate < return type > < delegate_name > ( <parameters>)

説明: 上記の構文では、アクセス修飾子はパブリック、プライベート、または保護される可能性があるため、デリゲートを宣言する前に宣言する必要があります。ここでデリゲートを宣言するには、キーワード delegate の後に関数の戻り値の型を使用する必要があります。たとえば、

public delegate void Show ( char ch );

上で使用した show デリゲートは、関数 Show () に関連付けられた同じパラメーターと戻り値の型を持つメソッドを指すために使用されます。

C# デリゲートの実装例

以下に挙げる例を示しますL

例 #1

シングルキャストデリゲートの動作を示すコード:

コード:

using System;
class Singlecast_Delegates
{
public delegate void Delete_func() ;
public class SD
{
public static void text_display()
{
Console.WriteLine ( " Hey hello ! , How are you " ) ;
}
public static void text_show()
{
Console.WriteLine ( " Hi ! How is everything ? " ) ;
}
public void print()
{
Console.WriteLine ( " Print " ) ;
}
}
static void Main(string[] args)
{
Delete_func del_var1 = SD.text_show ;
Delete_func del_var2 = new Delete_func ( SD.text_display ) ;
SD obj = new SD() ;
Delete_func del_var3 = obj.print ;
del_var1() ;
del_var2() ;
del_var3() ;
Console.ReadLine () ;
}
}

出力:

C# デリゲート

説明: 上記のコードでは、クラス SD の静的メソッド text_show() をデリゲート Delete_func() に割り当て、次にクラス SD の静的メソッド text_display() をデリゲート Delete_func に割り当てていることがわかります。 () new 演算子を使用します。さらに、両方の方法を使用してデリゲート関数を割り当てることができます。したがって、最初にクラス SD のインスタンスを作成し、メソッド print() をクラス付きのデリゲートを意味するデリゲートに割り当てました。最終的に、コード内で作成した関数を 1 つずつ呼び出せるように、SD クラスのオブジェクトを作成しました。

例 #2

ダブルキャストデリゲートの動作を示すコード:

コード:

using System ;
namespace Educba {
// Here we are declaring the class with name " Edu "
class Edu {
// In this class we will declare the delegates
// Here the return type and parameter type must be same as the return and parameter type
// of the 2 methods
// "number_addition" and "number_substraction" are 2 given delegate names by user
public delegate void number_addition ( int x , int y ) ;
public delegate void number_substraction ( int x , int y ) ;
// here we are declaring the "total" method
public void total ( int x , int y )
{
Console.WriteLine( " (50 + 10) = {0} " , x + y ) ;
}
// here we are declaring the "substraction" method
public void substraction ( int x , int y )
{
Console.WriteLine( " ( 95 - 30 ) = {0} ", x - y ) ;
}
// Main Method declaration
public static void Main(String []args)
{
// creating an object " obj " for "Edu"
Edu obj = new Edu() ;
number_addition delegate1 = new number_addition ( obj.total ) ;
number_substraction delegate2 = new number_substraction( obj.substraction ) ;
// creating an object of the delegate class named as " delegate1 "
// for method "total" and "delegate2" for method "substraction" &
// pass the parameter of the  2 methods by class object "obj"
// by instantiating the delegates
// passing the below values to the methods by declared delegate object
delegate1( 50 , 10) ;
delegate2( 95 , 30);
}
}
}

出力:

C# デリゲート

説明: 上記のコードでは、シングルキャスト デリゲートとは異なるダブルキャスト デリゲートを使用していることがわかります。 Edu という名前のクラスを宣言し、このクラスでは 2 つのデリゲートを宣言しました。1 つは一度に 2 つの整数を加算するもので、もう 1 つは指定された 2 つの整数を減算するものです。その後、追加デリゲートの結果を保存するためのメソッド total を宣言しました。同様に、減算デリゲートの結果を格納するメソッドをもう 1 つ宣言しました。メイン クラスでは、与えられた 2 つの整数に対して加算と減算を実行する関数のデリゲートを呼び出すことができるように、Edu クラスのオブジェクトを作成しています。値を渡して結果を取得します。

例 #3

匿名デリゲートの動作を示すコード:

コード:

using System;
// declaring delegate method Demo of void type
public delegate void Demo() ;
// creating a class for declaring a static method inside this class.
public class First_Program
{
static int Main()
{
Demo Display = delegate()
{  // displaying the output on the user screen
Console.WriteLine ( " Here is the Anonymous delegate method " ) ;
};
// Here we are calling the display function.
Display() ;
return 0 ;
}
}

出力:

C# デリゲート

結論

コーダーがメソッドを他のメソッドの引数として渡す必要がある場合は常にデリゲートを使用します。または、デリゲートは関数シグネチャをカプセル化できるクラスであると言えます。これは、C# のメソッド パラメーターに名前を付けることに似ています。

以上がC# デリゲートの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:C# でのアサート次の記事:C# でのアサート