LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 委托

admin
2017年3月8日 0:16 本文热度 6121

C#中的委托类似于C++中的函数指针.可以根据定义的委托实现不同的函数.定义委托的方法如下

[csharp] view plain copy
 print?
  1. [修饰符]delegate 返回类型 委托名([参数列表])  

其中修饰符合参数列表是可选项.使用的时候先声明一个委托.例如:定义一个返回类型为void的,需要一个变量的委托

[csharp] view plain copy
 print?
  1. delegate void MyDelegate(object o1)  

然后在定义需要使用该委托的方法,这个方法必须和声明的委托具有相同的签名,即返回类型相同,而且需要传递一个参数.

  1. private void Myfunction1(object o1)  
  2. {  
  3.    string str = (string)o1;  
  4.    /*do something */  
  5. }  
  6.   
  7. private void Myfunction2(object o2)  
  8. {  
  9.     int num = (int)o2;  
  10.     /* do something */  
  11. }  

使用方法:

MyDelegate mydelegate1 = new MyDelegate (Myfunction1);

MyDelegate mydelegate2 = new MyDelegate (Myfunction2);

mydelegate1 ("This is a demon!");

mydelegate2 (25);

委托在使用的时候必须要实例化,这样就实现了一个委托实例化不同的函数。

附上两个实例:

第一个:一个雇员类,实现按年龄和薪金排序

[csharp] view plain copy
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace 委托  
  8. {  
  9.       
  10.     /// <summary>  
  11.     /// 定义一个委托用于比较  
  12.     /// </summary>  
  13.     /// <param name="o1"></param>  
  14.     /// <param name="o2"></param>  
  15.     /// <returns></returns>  
  16.     delegate bool Compare(object o1, object o2);  
  17.     class Employee  
  18.     {  
  19.         private int mAge;  
  20.         private int mSalary;  
  21.         private string mName;  
  22.   
  23.         public Employee(string aName, int aAge, int aSalary)  
  24.         {  
  25.             this.mAge = aAge;  
  26.             this.mSalary = aSalary;  
  27.             this.mName = aName;  
  28.         }  
  29.         public void Print()  
  30.         {  
  31.             Console.WriteLine("Name is:{0}, Age is:{1}, Salary is:{2}", mName, mAge, mSalary);  
  32.         }  
  33.         static public bool CompareAge(object o1, object o2)  
  34.         {  
  35.             Employee e1 = (Employee)o1;  
  36.             Employee e2 = (Employee)o2;  
  37.             return (e1.mAge > e2.mAge) ? true : false;  
  38.         }  
  39.         static public bool CompareSalary(object o1, object o2)  
  40.         {  
  41.             Employee e1 = (Employee)o1;  
  42.             Employee e2 = (Employee)o2;  
  43.             return (e1.mSalary > e2.mSalary) ? true : false;  
  44.         }  
  45.     }  
  46.       
  47.     class Test  
  48.     {  
  49.         static public void Sort(object[] aSortArry, Compare CompareMethod)  
  50.         {  
  51.             for (int i=0; i<aSortArry.Length; i++)  
  52.             {  
  53.                 for (int j = i + 1; j < aSortArry.Length; j++)  
  54.                 {  
  55.                     if (CompareMethod(aSortArry[i], aSortArry[j]))  
  56.                     {  
  57.                         object temp = aSortArry[i];  
  58.                         aSortArry[i] = aSortArry[j];  
  59.                         aSortArry[j] = temp;  
  60.                     }  
  61.                 }  
  62.             }  
  63.         }  
  64.         static void Main(string[] args)  
  65.         {  
  66.             Employee[] employee = {  
  67.               new Employee("Wang",12,800), new Employee("Liu", 21, 900), new Employee("Li", 34, 300),  
  68.               new Employee("Sun", 9, 1200), new Employee("Xu", 23, 2000),new Employee("Wu", 21, 400)};  
  69.             Compare CompareAge = new Compare(Employee.CompareAge);  
  70.             Compare CompareSalary = new Compare(Employee.CompareSalary);  
  71.             Console.WriteLine("Sorted by age:");  
  72.             Sort(employee, CompareAge);  
  73.             for (int i = 0; i < employee.Length; i++)  
  74.                 employee[i].Print();  
  75.             Console.WriteLine("Sorted by salary:");  
  76.             Sort(employee, CompareSalary);  
  77.             for (int i = 0; i < employee.Length; i++)  
  78.                 employee[i].Print();         
  79.             Console.ReadKey();  
  80.         }  
  81.     }  
  82.        
  83.       
  84. }  

第二个:

[csharp] view plain copy
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace 委托  
  8. {  
  9.       
  10.     delegate void None();  
  11.     delegate void Single(object o1);  
  12.     delegate void Complex(object o1, object o2);  
  13.     class Test  
  14.     {  
  15.         static private void DeleNone()  
  16.         {  
  17.             Console.WriteLine("This is the none delegate!");  
  18.         }  
  19.         static private void DeleSingle(object o1)  
  20.         {  
  21.             string str = (string)o1;  
  22.             Console.WriteLine(str);  
  23.         }  
  24.         static private void DeleComplex(object o1, object o2)  
  25.         {  
  26.             string str1 = (string)o1;  
  27.             string str2 = (string)o2;  
  28.             Console.WriteLine(str1 + str2);  
  29.         }  
  30.         static void Main(string[] args)  
  31.         {  
  32.             None mNone = new None(DeleNone);  
  33.             Single mSingle = new Single(DeleSingle);  
  34.             Complex mComplex = new Complex(DeleComplex);  
  35.             mSingle("This is the Single delegate!");  
  36.             mComplex("I am o1 ""I am o2");  
  37.             Console.ReadKey();  
  38.         }  
  39.     }  
  40. }  


该文章在 2017/3/8 0:16:58 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved