.NET框架中的委托和事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text.RegularExpressions; namespace _.net之美
{
class Program
{
static void Main(string[] args)
{
Heater heater = new Heater();
Alert alert = new Alert();
heater.boiled += alert.AlertTemperature;
heater.boiled += Display.ShowMsg;
heater.BoilWater();
Console.ReadKey();
}
} public class Heater
{
private readonly string area="ChengDu";
public string Area { get { return area; } } private readonly string type = "";
public string Type { get { return type; } } private int temperature; public delegate void BoiledEventHandle(object sender, BoiledEventArgs args);
public event BoiledEventHandle boiled; public void BoilWater()
{
for (int i = ; i <= ; i++)
{
temperature = i;
BoiledEventArgs e = new BoiledEventArgs(temperature);
if (temperature > )
{
OnBoiled(e);
}
}
} private void OnBoiled(BoiledEventArgs e)
{
if (boiled != null)
boiled(this, e);
} public class BoiledEventArgs
{
private readonly int temperature;
public BoiledEventArgs(int temperature)
{
this.temperature = temperature;
} public int Temperature { get { return this.temperature; } }
}
} public class Alert
{
public void AlertTemperature(object sender, Heater.BoiledEventArgs e)
{
Heater heater = (Heater)sender;
Console.WriteLine("Area:{0},Type:{1}", heater.Area, heater.Type);
Console.WriteLine("水快烧开了!");
}
} public class Display
{
public static void ShowMsg(object sender, Heater.BoiledEventArgs e)
{
Heater heater = (Heater)sender;
Console.WriteLine("Area:{0},Type:{1}", heater.Area, heater.Type);
Console.WriteLine("当前温度:{0}", e.Temperature);
}
}
}

委托进阶

第3章 C#中的委托和事件的更多相关文章

  1. 【转载】C# 中的委托和事件(详解:简单易懂的讲解)

    本文转载自http://www.cnblogs.com/SkySoot/archive/2012/04/05/2433639.html C# 中的委托和事件(详解) C# 中的委托和事件 委托和事件在 ...

  2. C# 中的委托和事件(详解)

    C# 中的委托和事件 委托和事件在 .NET Framework 中的应用非常广泛,然而,较好地理解委托和事件对很多接触 C# 时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太 ...

  3. 【转载】C# 中的委托和事件(详解)

    <div class="postbody"> <div id="cnblogs_post_body" class="blogpost ...

  4. C# 中的委托和事件

    觉得这篇文章写的非常好,大神之作,由简入繁,对我这种初学者来说帮忙很大,特此留存下. 摘自:http://tracefact.net/CSharp-Programming/Delegates-and- ...

  5. C# 中的委托和事件(转)

    引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...

  6. C# 中的委托和事件(转载)

    引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...

  7. 【转】C# 中的委托和事件

    阅读目录 C# 中的委托和事件 引言 将方法作为方法的参数 将方法绑定到委托 事件的由来 事件和委托的编译代码 委托.事件与Observer设计模式 .Net Framework中的委托与事件 总结 ...

  8. 分分钟用上C#中的委托和事件之窗体篇

    上次以鸿门宴的例子写了一篇名为<分分钟用上C#中的委托和事件>的博文,旨在帮助C#初学者迈过委托和事件这道坎,能够用最快的速度掌握如何使用它们.如果觉得意犹未尽,或者仍然不知如何在实际应用 ...

  9. 《C#高级编程》学习笔记------C#中的委托和事件(续)

    本文转载自张子阳 目录 为什么要使用事件而不是委托变量? 为什么委托定义的返回值通常都为void? 如何让事件只允许一个客户订阅?(事件访问器) 获得多个返回值与异常处理 委托中订阅者方法超时的处理 ...

随机推荐

  1. lsm-tree

    https://www.quora.com/How-does-the-Log-Structured-Merge-Tree-work http://blog.cloudera.com/blog/2012 ...

  2. SQL Server中常用的SQL语句(转):

    SQL Server中常用的SQL语句 转自:http://www.cnblogs.com/rainman/archive/2013/05/04/3060428.html 1.概述 名词 笛卡尔积.主 ...

  3. Abstract和Virtual和interface , 派生类中重写 override / new关键字

    http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html C#中Abstract和Virtual 在C#的学习中,容易混淆virtua ...

  4. Mysql 自定义HASH索引带来的巨大性能提升----[真相篇]

    推倒重来 俗话说no zuo no die why you try,这时候我又忍不住zuo了,吭哧吭哧的把解决过程发上博客,向全世界宣布,哥又搞定个难题. 剧情的发展往往是看起来主角完全掌握了局势的情 ...

  5. JavaScript中的Array

    Array类型是ECMAScript 用的最多的类型了,ECMAScript中的数组每一项可以保存任何类型的数据,也就是说,数组的第一个项保存字符串,用第二个保存数值,用第三个位置来保存对象.二千数据 ...

  6. mysql 联合索引(转)

    http://blog.csdn.net/lmh12506/article/details/8879916 mysql 联合索引详解 联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中 ...

  7. 网络换行符替换为word换行符

    在替换的页面上,查找里输入:^l,在替换里输入:^p,然后点击替换即可.

  8. 在IIS8添加WCF服务支持

    最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF ...

  9. 42. Subsets && Subsets II

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  10. C++使用VARIANT实现二维数组的操作

    C++使用VARIANT实现二维数组的操作 VARIANT变量是COM组件之间互相通信的重要的参数变量之一,它可以容纳多种不同的类型,如short.long.double等,包括各类指针和数组.组件之 ...