C# 代表

WBOY
發布: 2024-09-03 15:29:52
原創
449 人瀏覽過

當我們想要處理程式碼中的任何事件或回調,或者我們可以說一個函數具有多個不同資料類型的參數但我們想要傳遞函數本身而不是參數時,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() 指派給了委託,這意味著帶有類別的委託。最後,我們為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 個給定的整數。之後,我們聲明了一個方法 Total 來儲存加法委託的結果。以同樣的方式,我們聲明了另一種方法來儲存減法委託的結果。在主類別中,我們建立 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中文網其他相關文章!

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