New keyword in C # several usage

Some time ago a friend asked about the C # New keywords have several uses, though often used this little guy in their daily programming, but it has several uses in the end really do not pay attention to before, will now summarize the information from the Internet in mind for colleagues under study.
 
(1) new operator is used to create an object and call the constructor.
 
(2) new modifier for inherited members hide base class members.
 
(3) new constraint for constraining type parameter may be used as parameters in the generic type declaration.
 
new operator
 
1 and is used to create an object constructor is called
 
Example: Class_Test Class_Test MyClass = new ();
 
2. also used to call the default value type constructor
 
Example: int of myInt = new int ();
 
of myInt initialized to 0 it is the default value of type int. The effect is equivalent to the statement: int = 0 of myInt;
 
3. not overload the new operator.
 
4. If the new operator fails to allocate memory, it throws in OutOfMemoryException.
 
The new modifier
 
to use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare that members of the same name in a derived class, and modify it with the new modifier.
 
Consider the following categories:

 1 public class MyClass
 2  
 3  {
 4  
 5      public int x;
 6   
 7      public void Invoke() {}
 8   
 9  }
10

Invoke the name in a derived class declaration with members hides the Invoke method in the base class, namely:

1 public class MyDerivedC : MyClass
2 {
3 
4    new public void Invoke() {}
5 
6 }

However, because the field x is not hidden by similar names, so it will not affect the field.
 
Hide name inheritance by one of the following forms:
 
1. Introducing a class or structure constant, specified attribute type or hides all base class members with the same name.
 
2. The introduction of a class or method hidden attribute structure base class with the same name, and the type field. Also hides the base class method with the same signature.
 
3. The introduction of a class or structure indexer hides all base class indexers with the same name.
 
4. On the same members use both new and override is wrong.
 
Note: Use the new modifier will generate a warning does not hide an inherited member declaration.
 
Example
 
In this example, nested classes MyClass hides the base class with the same name. This example illustrates not only how to use the fully qualified name of the class members to access hidden, but also shows a warning message on how to use the new modifier eliminated.

 1 using System;
 2 public class MyBaseC
 3 {
 4     public class MyClass
 5     {
 6         public int x = 200;
 7         public int y;
 8     }
 9 }
10 public class MyDerivedC : MyBaseC
11 {
12     new public class MyClass // nested type hiding the base type members    
13     {
14         public int x = 100;
15         public int y;
16         public int z;
17     }
18     public static void Main()
19     {
20         // Creating object from the overlapping class:    
21         MyClass S1 = new MyClass();
22         // Creating object from the hidden class:    
23         MyBaseC.MyClass S2 = new MyBaseC.MyClass();
24         Console.WriteLine(S1.x);
25         Console.WriteLine(S2.x);
26     }
27 }

 

 

 

 

Reproduced in: https: //my.oschina.net/weisenz/blog/200612

Guess you like

Origin blog.csdn.net/weixin_34128501/article/details/91920889