Code C#: MultiCast Delegate - Cơ chế ủy quyền (Delegate) đa phương thức trong C#

Người đăng: share-nhungdieuhay on Thứ Năm, 4 tháng 4, 2013



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyMulticastDelegate{
    public delegate void MulticastDelegate(int x, int y);

    public class Vidu2{
        public static void Cong(int x, int y) {
            Console.WriteLine("Ban dang goi phuong thuc Cong()");
            Console.WriteLine("{0} + {1} = {2}", x,y,x+y);
        }
        public static void Nhan(int x, int y) {
            Console.WriteLine("Ban dang goi phuong thuc Nhan()");
            Console.WriteLine("{0} x {1} = {2}", x, y, x * y);
        }
    }
    
    class Program{
        static void Main(string[] args){
            MulticastDelegate del = new MulticastDelegate(Vidu2.Cong);
            del += new MulticastDelegate(Vidu2.Nhan);
            Console.WriteLine("Goi dong thoi 2 phuong thuc Cong va Nhan\n\n");
            del(7, 6);

            del -= new MulticastDelegate(Vidu2.Cong);
            Console.WriteLine("\n\n****Da xoa phuong thuc Cong, chi goi phuong thuc Nhan****\n\n");
            del(4, 5);
            Console.ReadLine();
        }
    }
}


{ 0 nhận xét... read them below or add one }

Đăng nhận xét