惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
月光博客
月光博客
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
U
Unit 42
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
B
Blog
C
Check Point Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
量子位
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 旴江老段

如何开发和维能hold住全场的软件 什么样的软件才能hold住全场 SNOOP 开发WPF的好工具 被遗忘的事物 前台线程和后台线程(Foreground and Background Threads) runtime binding policy Checking space used in a database AsyncCallback方法和主线程怎么同步呢? Remoting Practice Sample Using Custom Assemblies with Reports 为什么32位的CPU?为什么32位的CPU只能支持4G的内存呢? 学习SSL和certificate的好网页 Wix Upgrade怎么判断是否更新 学WIX的好网站 SELECT @local_variable (Transact-SQL) sql server try catch and transaction的几个要点 使SQL关键字变大写的小工具 Assert.AreEqual .net 异步调用机制
委托和事件的区别
旴江老段 · 2012-04-20 · via 博客园 - 旴江老段

我想我知道委托和时间的区别了

委托可以被外部调用 

 namespace GrammerTest

{
    public delegate void FuncDelegate();
    public class DelegateTest
    {
        public FuncDelegate FuncDelegateObject;
        public  DelegateTest()
        {
            FuncDelegateObject = Func1;
            FuncDelegateObject += Func2;
        }

        public void Func1()
        {
            Console.WriteLine("Func 1");
        }

        public void Func2()
        {
            Console.WriteLine("Func 2");
        }
    }
}

         private static void Main()

        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //new ForeachNullList().Run();

            new DelegateTest().FuncDelegateObject();

            Console.Read();
            //Application.Run(new Form1());
        }

事件不可以从类的外部发动

 using System;

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

namespace GrammerTest
{
    public delegate void ADelegate();
    public class EventPublisher
    {
        public event ADelegate AEvent;
        public void Fire()
        {
            if (AEvent != null)
                AEvent();
        } 
  
    }

    internal class EventClient
    {

        public void Func1()
        {
            Console.WriteLine("Func 1");
        }
    }

    internal class EventClient2
    {
        public void Func2()
        {
            Console.WriteLine("Func 2");
        }
    }

    class EventTest
    {
        public void Run()
        {
            EventPublisher ep = new EventPublisher();
            EventClient ec1 = new EventClient();
            EventClient2 ec2 = new EventClient2();
            ep.AEvent += ec1.Func1;
            ep.AEvent += ec2.Func2;

            //ep.AEvent(); doesn't work
            
//only
            ep.Fire();
        }
    }

}