若要重载某个运算符,可以编写一个函数,在其命名运算符之后加上要重载的运算符的符号。例如,可按以下方法重载 + 运算符:
public static ComplexNumber operator+(ComplexNumber a, ComplexNumber b)
索引器
public class Department { private string name; private const int MAX_EMPLOYEES = 10; private string[] employees = new string[MAX_EMPLOYEES]; //employee array public Department(string departmentName) //constructor { name = departmentName; } public string this [int index] //indexer { get { if (index >= 0 && index < MAX_EMPLOYEES) { return employees[index]; } else { throw new System.IndexOutOfRangeException(); } } set { if (index >= 0 && index < MAX_EMPLOYEES) { employees[index] = value; } else { throw new System.IndexOutOfRangeException(); } } } // code for the rest of the class... }属性(相当于Annotation)public class Utilities : System.Web.Services.WebService { [System.Web.Services.WebMethod] // Attribute public string GetTime() { return System.DateTime.Now.ToShortTimeString(); } }[System.Serializable()] public class Employee { public int ID; public string Name; [System.NonSerialized()] public int Salary; }委托类似地,若要为任何带字符串参数且返回 long 型结果的函数创建委托,则使用以下语法:
然后,可以将此委托分配到带有此签名的任何方法,如下所示:
public static long DoWork( string name)调用委托相当简单。只需用委托变量的名称替代方法名称。这将使用值 11 和 22 调用 Add 方法,并返回一个 long 型结果,该结果被赋给变量 sum:Del operation; // declare the delegate variable operation = Add; // set the delegate to refer to the Add method long sum = operation(11, 22); // invoke the delegatepublic class MathClass { public static long Add(int i, int j) // static { return (i + j); } public static long Multiply (int i, int j) // static { return (i * j); } } class TestMathClass { delegate long Del(int i, int j); // declare the delegate type static void Main() { Del operation; // declare the delegate variable operation = MathClass.Add; // set the delegate to refer to the Add method long sum = operation(11, 22); // use the delegate to call the Add method operation = MathClass.Multiply; // change the delegate to refer to the Multiply method long product = operation(30, 40); // use the delegate to call the Multiply method System.Console.WriteLine("11 + 22 = " + sum); System.Console.WriteLine("30 * 40 = " + product); } }事件public class EventClass { // Declare the delegate type: public delegate void CustomEventHandler(object sender, System.EventArgs e); // Declare the event variable using the delegate type: public event CustomEventHandler CustomEvent; public void InvokeEvent() { // Invoke the event from within the class that declared the event: CustomEvent(this, System.EventArgs.Empty); } } class TestEvents { private static void CodeToRun(object sender, System.EventArgs e) { System.Console.WriteLine("CodeToRun is executing"); } private static void MoreCodeToRun(object sender, System.EventArgs e) { System.Console.WriteLine("MoreCodeToRun is executing"); } static void Main() { EventClass ec = new EventClass(); ec.CustomEvent += new EventClass.CustomEventHandler(CodeToRun); ec.CustomEvent += new EventClass.CustomEventHandler(MoreCodeToRun); System.Console.WriteLine("First Invocation:"); ec.InvokeEvent(); ec.CustomEvent -= new EventClass.CustomEventHandler(MoreCodeToRun); System.Console.WriteLine("/nSecond Invocation:"); ec.InvokeEvent(); } }