C# アクション デリゲート

WBOY
リリース: 2024-09-03 15:31:39
オリジナル
832 人が閲覧しました

組み込みデリゲートは、System 名前空間で定義されたジェネリック型に属します。これは戻り値を持たないメソッドで使用できます。つまり、それらはアクション デリゲートと呼ばれます。アクション デリゲートには、最小 1 個、最大 16 個の入力パラメータを含めることができ、これらのパラメータは同じデータ型または異なるデータ型を持つことができます。プログラム内でアクション デリゲートを使用すると、プログラムがより最適化され、読みやすくなります。このトピックでは、C# アクション デリゲートについて学習します。

C# のアクション デリゲートの構文は次のとおりです。

public delegate void Action < in input_parameter_type > (input_parameter_type   object);
public delegate void Action < in input_parameter_type1, in input_parameter_type2 >( input_parameter_type1 argument1, input_parameter_type2 argument2);
ログイン後にコピー

input_paramter_type、input_paramter_type1、input_paramter_type2 は入力パラメーターの型を表し、argument1 と argument2 はアクション デリゲートによってカプセル化されたメソッドで使用されるパラメーターです。

C# でのアクション デリゲートの動作

  • 戻り値を含まないメソッド、つまり戻り値の型が void のメソッドでアクション デリゲートを使用する必要がある場合、それをアクション デリゲートと呼びます。
  • アクション デリゲートはジェネリック型に属し、System 名前空間内で定義されます。
  • アクション デリゲートに含めることができる入力パラメーターの最小数は 1 で、アクション デリゲートに含めることができる入力パラメーターの最大数は 16 で、使用されるパラメーターのタイプは同じデータであることができます。型または異なるデータ型。
  • プログラム内でアクション デリゲートを利用することにより、プログラムはより最適化され、読みやすくなります。

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

例 #1

指定された文字列を連結し、ステートメントを出力として画面に出力するアクション デリゲートをデモする C# プログラム。

コード:

using System;
//a class called check is defined
class check
{
// a method called join is called which takes the parameter passed to the method and prints it as the output on the screen
public static void join(string str)
{
Console.WriteLine("Welcome to {0}", str);
}
// main method is called within which the join function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes one input parameter which is passed to the join method
Action<string> stringvalue = join;
stringvalue("C#");
}
}
ログイン後にコピー

出力:

C# アクション デリゲート

上記のプログラムは、「check」というクラスを定義し、「join」というメソッドを呼び出します。このメソッドは、渡されたパラメータを出力として画面に表示します。次に、main メソッドはアクション デリゲートを定義して「join」関数を呼び出します。次に、プログラムは 1 つの入力パラメーターを取るアクション デリゲートを定義します。

例 #2

指定された数値のべき乗を計算するアクション デリゲートを示す C# プログラム。

コード:

using System;
//a class called check is defined
class check
{
// a method called power is defined which takes two parameters passed to the method and calculates the power of the given number and displays it on the screen
public static void power(double number1, double number2)
{
Console.WriteLine("The power of the given number is {0}", Math.Pow(number1, number2));
}
// main method is called within which the power function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes two input parameters which is passed to the power method
Action<double, double> doublevalue = power;
doublevalue(2,2);
}
}
ログイン後にコピー

出力:

C# アクション デリゲート

提供されたプログラムでは、「check」という名前のクラスを定義します。このクラスには、渡された 2 つの指定されたパラメーターの累乗を計算し、結果を画面に表示する「power」という名前のメソッドが含まれています。その後、main メソッドを呼び出します。ここでは、アクション デリゲートを定義して「power」関数を呼び出します。また、2 つの入力パラメーターを受け入れるアクション デリゲートも定義します。

例 #3

指定された数値の 2 乗を求めるアクション デリゲートをデモする C# プログラム。

コード:

using System;
//a class called check is defined
class check
{
// a method called power is defined which takes two parameters passed to the method and calculates the power of the given number and displays it on the screen
public static void square(int number)
{
Console.WriteLine("The square of the given number is {0}", number * number);
}
// main method is called within which the power function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes one input parameter which is passed to the square method
Action<int> answer = square;
answer(3);
}
}
ログイン後にコピー

出力:

C# アクション デリゲート

上記のプログラムでは、「check」というクラスを定義しています。このクラス内で、2 つのパラメーターを入力として受け取る「power」という名前のメソッドを定義します。このメソッドは、指定された数値のべき乗を計算し、結果を画面に表示します。次に、main メソッドを呼び出します。ここでは、アクション デリゲートを定義して「power」関数を呼び出します。具体的には、2 つの入力パラメーターを受け入れるアクション デリゲートを定義します。

結論

このチュートリアルでは、定義を通じて C# のアクション デリゲートの概念、アクション デリゲートの構文、プログラミング例とその出力を通じて C# のアクション デリゲートの動作を理解します。

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

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!