【正文】
字一起使用的委托。 ? 定義一個或多個將方法連接到事件的類。 public interface I { event MyDelegate MyEvent。 } } 事件 (4/5) public delegate void EventHandler(object sender, e)。 public class Form1 { public Form1() { // Add Button1_Click as an event handler for Button139。數(shù)組元素通過計算所得的索引訪問。 數(shù)組 (2/3) ? 值類型數(shù)組和引用類型數(shù)組 ? 請看下列數(shù)組聲明: ? MyType[] myArray = new MyType[10]。 數(shù)組 (3/3) ? 與所有的 out 參數(shù)一樣,在使用數(shù)組類型的 out 參數(shù)前必須先為其賦值,即必須由接受方為其賦值。 struct 是值類型,而 class 是引用類型。 ? 結(jié)構(gòu)不像類那樣存在繼承。 類和結(jié)構(gòu) class CPoint { int x, y。 } } } Button b = new Button()。 listBox[0] = hello。然后,程序員可以將這種屬性附加到各種程序?qū)嶓w,而且在運行時環(huán)境中還可以檢索這些屬性信息。在語法上,屬性的指定方法為:將括在方括號中的屬性名置于其適用的實體聲明之前。然后,可以調(diào)用類型的方法或訪問其字段和屬性。 反射 反射通常具有以下用途(續(xù)): ? 使用 FieldInfo 來了解如下的類似信息:字段的名稱、訪問修飾符(如 public 或 private)和實現(xiàn)詳細信息(如 static)等;并獲取或設(shè)置字段值。 ? // Gets the type names from the assembly. ? Type [] types2 = ()。 反射 ? 下例顯示在獲取程序集的 Assembly 對象和模塊時所必需的語法。 ? 使用 ConstructorInfo 了解如下的類似信息:構(gòu)造函數(shù)的名稱、參數(shù)、訪問修飾符(如 public 或 private)和實現(xiàn)詳細信息(如 abstract 或 virtual)等。 ? 某些屬性對于給定實體可以指定多次。 [XmlElement(ment)] public string Comment。使用索引器時所用的語法與使用數(shù)組時所用的語法完全相同。 public string this[int index] { get { return items[index]。 10 20 sp cp 10 20 CPoint Properties ? Properties are “smart fields” ? Natural syntax, accessors, inlining public class Button: Control { private string caption。 ? 與 C++ 不同,無法使用 struct 關(guān)鍵字聲明類。在此情況下,結(jié)構(gòu)的成本較低??梢詫?shù)組類型的 ref 參數(shù)更改為調(diào)用的結(jié)果。 ? 將數(shù)組作為參數(shù)傳遞 ? 可以將初始化的數(shù)組傳遞給方法。數(shù)組可以是一維數(shù)組或多維數(shù)組。 } public void Disconnect() { = new EventHandler(Button1_Click)。在 Button 類的內(nèi)部, Click 成員如同一個 EventHandler 類型的私有字段。 } } public class MainClass { static private void f() { (This is called when the event fires.)。 ? 使用定義的構(gòu)造函數(shù),創(chuàng)建包含事件定義的類對象。否則,該邏輯必須放置在引發(fā)此事件的代碼中。只定義一個而不定義另一個是錯誤的。 } public static void DelegateFunction(int i) { (Called by delegate with number: {0}., i)} } ? 輸出 Called by delegate with number: 21. 事件 (1/5) 指定一個事件: [attributes] [modifiers] event type declarator。如果參數(shù)是一個指針,則必須用 unsafe 修飾符聲明委托。委托大致類似于 C++ 中的函數(shù)指針;但是,委托是類型安全和可靠的。委托實例封裝一個調(diào)用列表,該列表列出一個或多個方法,其中每個方法均作為一個可調(diào)用實體來引用。 (arg)。不過,無法定義僅在 ref 和 out 方面不同的重載。使用 out 參數(shù)的方法仍然可以返回一個值。 ? 傳遞到 ref 參數(shù)的參數(shù)必須最先初始化。 } ? 接口可以由類和結(jié)構(gòu)實現(xiàn)。有關(guān)屬性和屬性類的更多信息,請參見 17. 屬性。 類 (2/2) ? 與 C++ 不同, C 中僅允許單個繼承。 private 訪問僅限于包含類型。例如,用于生成圖形用戶界面的框架可以提供“控件”類和“窗體”類,這些類通過使用具有內(nèi)部訪問能力的成員進行合作。 ? 對于用戶定義類型,使用 new 調(diào)用默認構(gòu)造函數(shù)。 ? 可使用文字初始化簡單類型。 ? 所有的值類型均隱式派生自 Object 類。 (1, one)。 enum MyEnum { } namespace MyNamespace2 { } class MyClass2 { public static void Main(string[] args) { } } } 通用類型系統(tǒng) ? Everything is an object ? All types ultimately inherit from object ? Any piece of data can be stored, transported, and manipulated with no extra work 通用類型系統(tǒng) ? Boxing ? Allocates box, copies value into it ? Unboxing ? Checks type of box, copies value out int i = 123。 // A Hello World! program in C class Hello (public) class Hello { { static void Main() public static void main(String args[]) { { . (“Hello World!”)。 ? C 還憑借以下功能,為您提供生成持久系統(tǒng)級組件的能力: ? 對集成現(xiàn)有代碼提供完全 COM/平臺支持。Vis