委托

修饰符 class 类名
public class aaaa

--------------------------------------
泛型集合缓存比字典缓存效率高
泛型缓存不能自己释放

定义泛型接口
public interFace IFace<T>
{

}
定义接口名称以I开头(命名规范)

---------------------------------------
委托与事件
public delegate void aaa (int num)
(委托) (返回类型) (委托名) (参数)

只要是委托就有参数,委托的变量可以执行。

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩展性。

namespace _CSharp_Console
{
    class Employee
    {
        private string name;
        private decimal salary;

        public Employee(string name, decimal salary)
        {
            this.name = name;
            this.salary = salary;
        }

        public override string ToString()
        {
            return string.Format(name + ",{0:C}", salary);
        }

        public static bool CompareSalary(object o, object o2)
        {
            Employee e = (Employee)o;
            Employee e2 = (Employee)o2;
            return (e.salary > e2.salary) ? true : false;
        }
    }

    class Program
    {
        delegate bool dCompare(object o, object o2);

        static void Sort(object[] sortArray, dCompare dMethod)
        {
            for (int i = 0; i < sortArray.Length; i++)
            {
                for (int j = i + 1; j < sortArray.Length; j++)
                {
                    if (dMethod(sortArray[i], sortArray[j]))
                    {
                        object temp = sortArray[i];
                        sortArray[i] = sortArray[j];
                        sortArray[j] = temp;
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            Employee[] emp =
                {
                    new Employee("Peter",100),
                    new Employee("Mary",2345),
                    new Employee("Jack",1539),
                    new Employee("Tom",5500),
                    new Employee("Kevin",(decimal)99.23),
                    new Employee("Jim",3420)
                };

            dCompare dcom = new dCompare(Employee.CompareSalary);
            Program.Sort(emp, dcom);
            for (int i = 0; i < emp.Length; i++)
            {
                Console.WriteLine(emp[i].ToString());
            }

            Console.ReadLine();
        }
    }
}

但是这个参数是别人传进来的
是给别人用的时候
你并不知道你需要传入的函数的名称
那么定义一个函数委托
就可以解决这个问题
委托 实际上 就是一个 函数 格式的 映射
更适用于Framework相对于Application
可以降低耦合性


委托是触发式的.比如在错误处理中,你当然可以用try..catch来处理,但并不能保证捕获所有的错误.你也不知道错误什么时候发生,所以你可以用委托来处理.

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Error += new System.EventHandler(this.ZTCPage_Error);
}
protected void ZTCPage_Error(object sender, System.EventArgs e)
{
string errMsg;
Exception currentError = Server.GetLastError();

errMsg = "<link rel="stylesheet" href="../Css/ZTC.CSS">";
errMsg += "<h1>Page Error</h1><hr/>An unexpected error has occurred on this page. The system " +
"administrators have been notified. Please feel free to contact us with the information " +
"surrounding this error.<br/>"+
"The error occurred in: "+Request.Url.ToString()+"<br/>"+
"Error Message: <font class="ErrorMessage">"+ currentError.Message.ToString() + "</font><hr/>"+
"<b>Stack Trace:</b><br/>"+
currentError.ToString();
errMsg += "<br><hr/><br><input type='button' value='返回前一页' onclick='history.back(-1);'>";

WriteErrorToText(Request.Url.ToString(),currentError.Message.ToString(),currentError.ToString());

Response.Write( errMsg );
Server.ClearError();
}

C#里没有指针(不考虑unsafe),delegate就相当于函数指针。有些地方你可能不得不用函数指针的时候,就可以使用delegate。如在C#里调用API函数createthread时就需要函数指针,就需要用到delegate

  

原文地址:https://www.cnblogs.com/nxj1997/p/11166019.html