首页 > 开发 > XML > 正文

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

2024-09-05 20:55:47
字体:
来源:转载
供稿:网友
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文件


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表