目录
语法
实现 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 教程
1414
52
Laravel 教程
1307
25
PHP教程
1254
29
C# 教程
1228
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