语法
实现属性
public string MyText
{
get {return mText;} //不创建 get 为只读
set {mText = value;} //不创建 set 为只写
}
索引
public int this[int index]
{
get {return IntArray[index];}
set {IntArray[index] = value;}
}
强类型集合属性
private ArrayList mWidgets = new ArrayList();
public Widget GetWidget(int I)
{
return (Widget)mWidgets[I];
}
public void SetWidget(int I,Widget Wid)
{
mWidgets.Insert(I,Wid);
}
委托
一个类型安全的函数指针,可以用来调用方法。
public delegate int myDelegate(double D);
事件
public delegate void aDelegate(double d);
public event aDelegate aEvent;
引发事件
aEvent(666);
激活事件
public event EventHandler Changed;
protected void OnChanged()
{
if (Changed == null)return;
Changed(this,System.EventArgs.Empty);
}
事件处理程序
Account.aEvent += new aDelegate(aEventFun); //可关联多个事件
例 button1.Click += new System.EventHandler(clickHandler);
运行时移除事件
Account.aEvent -= new aDelegate(aEventFun);
定义别名
using Diag = System.Dragnstics;
using范围
using (FormAbout form = new FormAbout())
{
form.ShowDialog();
}
对象需实现IDieposable接口
代码块结束时调用Dispose方法