当我们想要处理代码中的任何事件或回调,或者我们可以说一个函数具有多个不同数据类型的参数但我们想要传递函数本身而不是参数时,C# 委托发挥着重要作用。在这种情况下,委托出现在图中,因为它的作用就像指向函数的指针,因为它是引用数据类型,因此它保存方法的引用。委托是 C# 中 System.Delegates 类的一部分。它们类似于 C 和 C++ 编程中的函数指针。
语法
让我们看一下 C# 中声明委托的语法
<access modifier> delegate ( <parameters>)</parameters></access>
说明:在上面的语法中,必须在声明委托之前声明访问修饰符,因为它可以是公共的、私有的或受保护的。现在,为了声明委托,我们必须使用关键字 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 () ; } }
输出:
说明:在上面的代码中,您可以看到我们将 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); } } }
输出:
说明:在上面的代码中,您可以看到我们使用的是双强制转换委托,这与单强制转换委托不同。我们已经声明了名为 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# 代表的详细内容。更多信息请关注PHP中文网其他相关文章!

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的编程过程包括以下步骤:1)编写C#代码,2)编译为中间语言(IL),3)由.NET运行时(CLR)执行。C#在.NET中的优势在于其现代化语法、强大的类型系统和与.NET框架的紧密集成,适用于从桌面应用到Web服务的各种开发场景。

C#是一种现代、面向对象的编程语言,由微软开发并作为.NET框架的一部分。1.C#支持面向对象编程(OOP),包括封装、继承和多态。2.C#中的异步编程通过async和await关键字实现,提高应用的响应性。3.使用LINQ可以简洁地处理数据集合。4.常见错误包括空引用异常和索引超出范围异常,调试技巧包括使用调试器和异常处理。5.性能优化包括使用StringBuilder和避免不必要的装箱和拆箱。

C#.NET应用的测试策略包括单元测试、集成测试和端到端测试。1.单元测试确保代码的最小单元独立工作,使用MSTest、NUnit或xUnit框架。2.集成测试验证多个单元组合的功能,常用模拟数据和外部服务。3.端到端测试模拟用户完整操作流程,通常使用Selenium进行自动化测试。

C#高级开发者面试需要掌握异步编程、LINQ、.NET框架内部工作原理等核心知识。1.异步编程通过async和await简化操作,提升应用响应性。2.LINQ以SQL风格操作数据,需注意性能。3..NET框架的CLR管理内存,垃圾回收需谨慎使用。

C#.NET面试问题和答案包括基础知识、核心概念和高级用法。1)基础知识:C#是微软开发的面向对象语言,主要用于.NET框架。2)核心概念:委托和事件允许动态绑定方法,LINQ提供强大查询功能。3)高级用法:异步编程提高响应性,表达式树用于动态代码构建。

C#.NET是构建微服务的热门选择,因为其生态系统强大且支持丰富。1)使用ASP.NETCore创建RESTfulAPI,处理订单创建和查询。2)利用gRPC实现微服务间的高效通信,定义和实现订单服务。3)通过Docker容器化微服务,简化部署和管理。

C#和.NET的安全最佳实践包括输入验证、输出编码、异常处理、以及身份验证和授权。1)使用正则表达式或内置方法验证输入,防止恶意数据进入系统。2)输出编码防止XSS攻击,使用HttpUtility.HtmlEncode方法。3)异常处理避免信息泄露,记录错误但不返回详细信息给用户。4)使用ASP.NETIdentity和Claims-based授权保护应用免受未授权访问。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

禅工作室 13.0.1
功能强大的PHP集成开发环境

WebStorm Mac版
好用的JavaScript开发工具