C#学习笔记之三(delegate, event, Make XML document

来源:互联网 发布:中国万网域名注册 编辑:程序博客网 时间:2024/06/10 05:25
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

delegates:
   // 1. nomal delegates = create before run, in main 
 //declare
 class Employee{}; class Couple{}
 
 public delegate bool Reverse(object lhs, object rhs);
 SetOrder(Reverse the delegatedFunction) {}
 //init
 Couple.Reverse employeeByID = new Couple.Reverse(Employee.SecondIDLower)
 Couple employees = new Couple(joe, fred);
 employees.SetOrder(employeeByID);
 
  // 2.static delegates = create it before run, static readonly
   // it will oppcupy memory
   class Employee {
    public static readonly Couple.Reverse EmployByID =
     new Couple.Reverse(Employee.SecondIDLower);
   }
   // call
   employees.SetOrder(Employee.EmployByID);
  // 3. delegates as propeties = create when use it
   // save memory
   class Employee {
    public static Couple.Reverse EmployeeByID {
     get {return new Couple.Reverse(Employee.SecondIDLower);}
    }
   }
  // 4. Muticast delegates
   // must return void
   public class delegateClass {
    public delegate void Stringdelegate(string s);
    public void Display(Stringdelegate d, string s
    {
     d(s);
    }
   }
   //declare
   delegateClass.Stringdelegate Writer, Logger, Storer;
   //init
   Writer = new delegateClass.Stringdelegate(Employee.WriteString);
   Logger = new delegateClass.Stringdelegate(Employee.LogString);
   Storer = new delegateClass.Stringdelegate(Employee.StoreString);
   ...
   public static void WriteString(string s) {}
   DeletegateClass.Stringdelegate mutiCast;
   mutiCast = Writer + Logger;
   mutiCase += Storer;
   mutiCast -= Logger;
   // call muticast delegates
   thedelegateClass.Display(mutiCast, joe.ToString);

event:
 //OfficeWatcher will subscrib Office's event
 public class OfficeWatcher()
 {
  //subscriber Office event
  public OfficeWatcher(Office)
  {
   office.OfficeChanged +=
    new Office.OfficeChangedHandler(OnOfficeChanged);
  }
  //OnOfficeChanged will call when Office changed
  public void OnOfficeChanged(object sender, Office eventArgs e) {}
 }
 //It will needed by NotifyOfficeChanged, hold data need to let
 //subscribers know
 public class OfficeeventArgs: eventArgs {}

 public class Office {
  //declare delegate, event will use it
  public delegate void OfficeChangedHandler(object sender,
  OffeventArgs e);
  //declare event
  public event OfficeChangedHandler OfficeChanged;
  //This method will go through event OfficeChanged event and
  //find who has subscribed this event, and let them know
  protected virtual void NotifyOfficeChanged(OfficeeventArgs e){}
    if (OfficeChanged != null) OfficeChanged(this, e);
  }
  ...
  public this[int index]
  {
   ...
   OfficeeventArgs e new OfficeeventArgs(ctr);
   //Notify subscribers Office has changed
   NotifyOfficeChanged(e);
  }
 }
 
XML document:
 1. generate XML comments in code using /// comments
 2. 项目属性-->配置属性-->生成-->输出-->XML文档文件
 3. ctrl+shift+B 生成项目, 这时生成XML文档
 4. 工具-->生成注释web页,这时生成html文件

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击