一、 new運算符
① 用於創建對象和調用構造函數。 例如:
- Class1 obj = new Class1();
② 還可用於創建匿名類型的實例:
- var query = from cust in customers
- select new {Name = cust.Name, Address = cust.PrimaryAddress};
③ new 運算符還用於調用值類型的默認構造函數。 例如:
- int i = new int();
在上一個語句中,i 初始化爲 0,它是 int 類型的默認值。 該語句的效果等同於:
- int i = 0;
值類型對象(例如結構)是在堆棧上創建的,而引用類型對象(例如類)是在堆上創建的。 兩種類型的對象都是自動銷燬的,但是,基於值類型的對象是在超出範圍時銷燬,而基於引用類型的對象則是在對該對象的最後一個引用被移除之後在某個不確定的時間銷燬。 對於佔用固定資源(例如大量內存、文件句柄或網絡連接)的引用類型,有時需要使用確定性終止以確保對象被儘快銷燬。
不能重載 new 運算符。
在下面的示例中,通過使用 new 運算符創建並初始化一個 struct 對象和一個類對象,然後爲它們賦值。 顯示了默認值和所賦的值。
- struct SampleStruct
- {
- public int x;
- public int y;
- public SampleStruct(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- class SampleClass
- {
- public string name;
- public int id;
- public SampleClass() {}
- public SampleClass(int id, string name)
- {
- this.id = id;
- this.name = name;
- }
- }
- class ProgramClass
- {
- static void Main()
- {
- // Create objects using default constructors:
- SampleStruct Location1 = new SampleStruct();
- SampleClass Employee1 = new SampleClass();
- // Display values:
- Console.WriteLine("Default values:");
- Console.WriteLine(" Struct members: {0}, {1}",
- Location1.x, Location1.y);
- Console.WriteLine(" Class members: {0}, {1}",
- Employee1.name, Employee1.id);
- // Create objects using parameterized constructors:
- SampleStruct Location2 = new SampleStruct(10, 20);
- SampleClass Employee2 = new SampleClass(1234, "Cristina Potra");
- // Display values:
- Console.WriteLine("Assigned values:");
- Console.WriteLine(" Struct members: {0}, {1}",
- Location2.x, Location2.y);
- Console.WriteLine(" Class members: {0}, {1}",
- Employee2.name, Employee2.id);
- }
- }
- /*
- Output:
- Default values:
- Struct members: 0, 0
- Class members: , 0
- Assigned values:
- Struct members: 10, 20
- Class members: Cristina Potra, 1234
- */
注意,在示例中字符串的默認值爲 null, 因此未顯示它。
二、 new修飾符
在用作修飾符時,new 關鍵字可以顯式隱藏從基類繼承的成員。 隱藏繼承的成員時,該成員的派生版本將替換基類版本。 雖然可以在不使用 new 修飾符的情況下隱藏成員,但會生成警告。 如果使用 new 顯式隱藏成員,則會取消此警告,並記錄要替換爲派生版本這一事實。
若要隱藏繼承的成員,請使用相同名稱在派生類中聲明該成員,並使用 new 修飾符修飾該成員。 例如:
- public class BaseC
- {
- public int x;
- public void Invoke() { }
- }
- public class DerivedC : BaseC
- {
- new public void Invoke() { }
- }
在此示例中,DerivedC.Invoke 隱藏了 BaseC.Invoke。 字段 x 不受影響,因爲它沒有被類似名稱的字段隱藏。
通過繼承隱藏名稱採用下列形式之一:
· 引入類或結構中的常數、指定、屬性或類型隱藏具有相同名稱的所有基類成員。
· 引入類或結構中的方法隱藏基類中具有相同名稱的屬性、字段和類型。 同時也隱藏具有相同簽名的所有基類方法。
· 引入類或結構中的索引器將隱藏具有相同名稱的所有基類索引器。
對同一成員同時使用 new 和 override 是錯誤的做法,因爲這兩個修飾符的含義互斥。 new 修飾符會用同樣的名稱創建一個新成員並使原始成員變爲隱藏的。 override 修飾符會擴展繼承成員的實現。
在不隱藏繼承成員的聲明中使用 new 修飾符將會生成警告。
在該例中,基類 BaseC 和派生類 DerivedC 使用相同的字段名 x,從而隱藏了繼承字段的值。 該示例演示了 new 修飾符的用法。 另外還演示瞭如何使用完全限定名訪問基類的隱藏成員。見:
- public class BaseC
- {
- public static int x = 55;
- public static int y = 22;
- }
- public class DerivedC : BaseC
- {
- // Hide field 'x'.
- new public static int x = 100;
- static void Main()
- {
- // Display the new value of x:
- Console.WriteLine(x);
- // Display the hidden value of x:
- Console.WriteLine(BaseC.x);
- // Display the unhidden member y:
- Console.WriteLine(y);
- }
- }
- /*
- Output:
- 100
- 55
- 22
- */
在此示例中,嵌套類隱藏了基類中同名的類。 此示例演示瞭如何使用 new 修飾符來消除警告消息,以及如何使用完全限定名來訪問隱藏的類成員。見:
- public class BaseC
- {
- public class NestedC
- {
- public int x = 200;
- public int y;
- }
- }
- public class DerivedC : BaseC
- {
- // Nested type hiding the base type members.
- new public class NestedC
- {
- public int x = 100;
- public int y;
- public int z;
- }
- static void Main()
- {
- // Creating an object from the overlapping class:
- NestedC c1 = new NestedC();
- // Creating an object from the hidden class:
- BaseC.NestedC c2 = new BaseC.NestedC();
- Console.WriteLine(c1.x);
- Console.WriteLine(c2.x);
- }
- }
- /*
- Output:
- 100
- 200
- */
如果移除 new 修飾符,該程序仍可編譯和運行,但您會收到以下警告:The keyword new is required on 'MyDerivedC.x' because it hides inherited member 'MyBaseC.x'.
三、 new約束
new 約束指定泛型類聲明中的任何類型參數都必須有公共的無參數構造函數。 如果要使用 new 約束,則該類型不能爲抽象類型。
當泛型類創建類型的新實例,請將 new 約束應用於類型參數,如下面的示例所示:
- class ItemFactory<T> where T : new()
- {
- public T GetNewItem()
- {
- return new T();
- }
- }
當與其他約束一起使用時,new() 約束必須最後指定:
- public class ItemFactory2<T>
- where T : IComparable, new()
- {
- }