NEW關鍵字的三種用法

 

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章