程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 委托筆記(一)

委托筆記(一)

編輯:C#入門知識

委托用於將方法作為參數傳遞給其他方法,委托應用的示例:
 
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.WriteLine("調用delegate d:");
        d("D");

        Console.ReadKey();
    }
}
 
輸出為:
調用delegate a:
Hello, A!
調用delegate b:
Goodbye, B!
調用delegate c:
Hello, C!
Goodbye, C!
調用delegate d:
Goodbye, D!

 


摘自 Fly to The Moon

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved