C# 動作委託

WBOY
發布: 2024-09-03 15:31:39
原創
998 人瀏覽過

內建委託屬於 System 命名空間下定義的泛型類型。您可以在沒有任何傳回值的方法中使用它,這表示它們被稱為操作委託。一個動作委託最少可以包含 1 個輸入參數,最多可以包含 16 個輸入參數,並且這些參數可以具有相同的資料類型或不同的資料類型。在程式中使用操作委託可以使其更加優化和可讀。在本主題中,我們將學習 C# Action Delegate。

C# 中 Action Delegate 的語法如下:

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是action delegate封裝的方法中使用的參數。

C# 中操作委託的工作

  • 當您需要在不包含任何傳回值的方法(即具有 void 傳回類型的方法)中使用操作委託時,您稱為操作委託。
  • 操作委託屬於泛型類型,並在系統命名空間內定義。
  • 一個動作委託中可以包含的輸入參數的最小數量為1 個,一個動作委託中可以包含的輸入參數的最大數量為16 個,並且使用的參數類型可以是相同的數據類型或不同的數據類型。
  • 透過在程式中使用動作委託,程式變得更加最佳化和可讀。

範例

以下是下面提到的範例:

範例#1

C# 程式示範 Action Delegate 連接給定字串並將語句印在螢幕上作為輸出。

代碼:

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」函數。然後,程式定義一個帶有一個輸入參數的操作委託。

範例#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」的類別。該類別包含一個名為“power”的方法,該方法計算傳遞給它的兩個給定參數的冪並將結果顯示在螢幕上。之後,我們呼叫 main 方法,其中透過定義操作委託來呼叫「power」函數。我們也定義了一個接受兩個輸入參數的操作委託。

範例 #3

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」的類別。在這個類別中,我們定義了一個名為「power」的方法,它接受兩個參數作為輸入。此方法計算給定數字的冪並將結果顯示在螢幕上。接下來,我們呼叫 main 方法,在其中透過定義操作委託來呼叫「power」函數。具體來說,我們定義一個接受兩個輸入參數的操作委託。

結論

在本教程中,我們透過定義來了解 C# 中操作委託的概念、操作委託的語法,以及透過程式設計範例及其輸出來了解 C# 中操作委託的工作原理。

以上是C# 動作委託的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!