目錄
文法
實作 C# 委託的範例
範例#3
結論

C# 代表

Sep 03, 2024 pm 03:29 PM
c# c# tutorial

當我們想要處理程式碼中的任何事件或回調,或者我們可以說一個函數具有多個不同資料類型的參數但我們想要傳遞函數本身而不是參數時,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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1655
14
CakePHP 教程
1413
52
Laravel 教程
1306
25
PHP教程
1252
29
C# 教程
1226
24
使用 C# 的活動目錄 使用 C# 的活動目錄 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

C# 序列化 C# 序列化 Sep 03, 2024 pm 03:30 PM

C# 序列化指南。這裡我們分別討論C#序列化物件的介紹、步驟、工作原理和範例。

C# 中的隨機數產生器 C# 中的隨機數產生器 Sep 03, 2024 pm 03:34 PM

C# 隨機數產生器指南。在這裡,我們討論隨機數產生器的工作原理、偽隨機數和安全數的概念。

C# 資料網格視圖 C# 資料網格視圖 Sep 03, 2024 pm 03:32 PM

C# 資料網格視圖指南。在這裡,我們討論如何從 SQL 資料庫或 Excel 檔案載入和匯出資料網格視圖的範例。

C# 中的階乘 C# 中的階乘 Sep 03, 2024 pm 03:34 PM

C# 階乘指南。這裡我們討論 C# 中階乘的介紹以及不同的範例和程式碼實作。

c#多線程和異步的區別 c#多線程和異步的區別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在這裡,我們討論 C# 中模式的介紹和前 3 種類型,以及其範例和程式碼實作。

C# 中的質數 C# 中的質數 Sep 03, 2024 pm 03:35 PM

C# 質數指南。這裡我們討論c#中素數的介紹和範例以及程式碼實作。

See all articles