当前位置:编程学习 > C#/ASP.NET >>

委托笔记(一)

委托用于将方法作为参数传递给其他方法,委托应用的示例:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DelegateApp
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    class Program
    {
        //定义FILTER委托     
        public delegate bool FilterDelegate(Person p);

        static void Main(string[] args)
        {
            //Create Person objects     
            Person p1 = new Person() { Name = "John", Age = 41 };
            Person p2 = new Person() { Name = "Jane", Age = 69 };
            Person p3 = new Person() { Name = "Jake", Age = 12 };
            Person p4 = new Person() { Name = "Jessie", Age = 25 };
            Person p5 = new Person() { Name = "Cherry", Age = 6 };

            //Create a list of Person objects and fill it     
            List<Person> people = new List<Person>() { p1, p2, p3, p4, p5 };

            DisplayPeople("儿童:", people, IsChild);
            DisplayPeople("易做图:", people, IsAdult);
            DisplayPeople("老人:", people, IsSenior);

            //调用方法不变,仅需增加委托调用的方法IsOnetoThree
            DisplayPeople("10岁~30岁之间:", people, IsOnetoThree);

            Console.Read();
        }

        /// <summary>  
        /// A method to filter out the people you need     
        /// </summary> 
        /// <param name="people">A list of people</param>  
        /// <param name="filter">A filter</param> 
        /// <returns>A filtered list</returns>   
        static void DisplayPeople(string title, List<Person> people, FilterDelegate filter)
        {
            Console.WriteLine(title);
            foreach (Person p in people)
            {
                if (filter(p))
                {
                    Console.WriteLine("{0}, {1} years old", p.Name, p.Age);
                }
            }
            Console.Write("\n\n");
        }

        //==========FILTERS===================    
        static bool IsChild(Person p)
        {
            return p.Age <= 18;
        }
        static bool IsAdult(Person p)
        {
            return p.Age >= 18;
        }
        static bool IsSenior(Person p)
        {
            return p.Age >= 65;
        }

        //扩展FILTERS
        static bool IsOnetoThree(Person p) 
        {
            return p.Age > 10 && p.Age < 30;
        }
    }
}
 
输出为:
儿童:
Jake, 12 years old
Cherry, 6 years old

易做图:
John, 41 years old
Jane, 69 years old
Jessie, 25 years old

老人:
Jane, 69 years old

10岁~30岁之间:
Jake, 12 years old
Jessie, 25 years old
由此可见,DisplayPeople方法更易扩展维护。
 
多路委托:
 
using System;

delegate void MyDelegate(string s);

class MyClass
{
    public static void Main()
    {
        MyDelegate a, b, c, d;

        // 委托a
        a = s => { Console.WriteLine("Hello, {0}!", s); };

        // 委托b
        b = s => { Console.WriteLine("Goodbye, {0}!", s); };

        // 委托c,合并a+b
        c = a + b;

        // 委托d,调用委托c,移除a
        d = c - a;

        Console.WriteLine("调用delegate a:");
        a("A");
        Console.Write("\n");
        Console.WriteLine("调用delegate b:");
        b("B");
        Console.Write("\n");
        Console.WriteLine("调用delegate c:");
        c("C");
        Console.Write("\n");
        Console.Wri

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,