C# 委托
|
admin
2017年3月8日 0:16
本文热度 6361
|
C#中的委托类似于C++中的函数指针.可以根据定义的委托实现不同的函数.定义委托的方法如下
- [修饰符]delegate 返回类型 委托名([参数列表])
其中修饰符合参数列表是可选项.使用的时候先声明一个委托.例如:定义一个返回类型为void的,需要一个变量的委托
- delegate void MyDelegate(object o1)
然后在定义需要使用该委托的方法,这个方法必须和声明的委托具有相同的签名,即返回类型相同,而且需要传递一个参数.
- private void Myfunction1(object o1)
- {
- string str = (string)o1;
-
- }
-
- private void Myfunction2(object o2)
- {
- int num = (int)o2;
-
- }
使用方法:
MyDelegate mydelegate1 = new MyDelegate (Myfunction1);
MyDelegate mydelegate2 = new MyDelegate (Myfunction2);
mydelegate1 ("This is a demon!");
mydelegate2 (25);
委托在使用的时候必须要实例化,这样就实现了一个委托实例化不同的函数。
附上两个实例:
第一个:一个雇员类,实现按年龄和薪金排序
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace 委托
- {
-
-
-
-
-
-
-
- delegate bool Compare(object o1, object o2);
- class Employee
- {
- private int mAge;
- private int mSalary;
- private string mName;
-
- public Employee(string aName, int aAge, int aSalary)
- {
- this.mAge = aAge;
- this.mSalary = aSalary;
- this.mName = aName;
- }
- public void Print()
- {
- Console.WriteLine("Name is:{0}, Age is:{1}, Salary is:{2}", mName, mAge, mSalary);
- }
- static public bool CompareAge(object o1, object o2)
- {
- Employee e1 = (Employee)o1;
- Employee e2 = (Employee)o2;
- return (e1.mAge > e2.mAge) ? true : false;
- }
- static public bool CompareSalary(object o1, object o2)
- {
- Employee e1 = (Employee)o1;
- Employee e2 = (Employee)o2;
- return (e1.mSalary > e2.mSalary) ? true : false;
- }
- }
-
- class Test
- {
- static public void Sort(object[] aSortArry, Compare CompareMethod)
- {
- for (int i=0; i<aSortArry.Length; i++)
- {
- for (int j = i + 1; j < aSortArry.Length; j++)
- {
- if (CompareMethod(aSortArry[i], aSortArry[j]))
- {
- object temp = aSortArry[i];
- aSortArry[i] = aSortArry[j];
- aSortArry[j] = temp;
- }
- }
- }
- }
- static void Main(string[] args)
- {
- Employee[] employee = {
- new Employee("Wang",12,800), new Employee("Liu", 21, 900), new Employee("Li", 34, 300),
- new Employee("Sun", 9, 1200), new Employee("Xu", 23, 2000),new Employee("Wu", 21, 400)};
- Compare CompareAge = new Compare(Employee.CompareAge);
- Compare CompareSalary = new Compare(Employee.CompareSalary);
- Console.WriteLine("Sorted by age:");
- Sort(employee, CompareAge);
- for (int i = 0; i < employee.Length; i++)
- employee[i].Print();
- Console.WriteLine("Sorted by salary:");
- Sort(employee, CompareSalary);
- for (int i = 0; i < employee.Length; i++)
- employee[i].Print();
- Console.ReadKey();
- }
- }
-
-
- }
第二个:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace 委托
- {
-
- delegate void None();
- delegate void Single(object o1);
- delegate void Complex(object o1, object o2);
- class Test
- {
- static private void DeleNone()
- {
- Console.WriteLine("This is the none delegate!");
- }
- static private void DeleSingle(object o1)
- {
- string str = (string)o1;
- Console.WriteLine(str);
- }
- static private void DeleComplex(object o1, object o2)
- {
- string str1 = (string)o1;
- string str2 = (string)o2;
- Console.WriteLine(str1 + str2);
- }
- static void Main(string[] args)
- {
- None mNone = new None(DeleNone);
- Single mSingle = new Single(DeleSingle);
- Complex mComplex = new Complex(DeleComplex);
- mSingle("This is the Single delegate!");
- mComplex("I am o1 ", "I am o2");
- Console.ReadKey();
- }
- }
- }
该文章在 2017/3/8 0:16:58 编辑过