Reference http://dev.yesky.com/276/7618276.shtml
C # the usage of the new keyword in programming is as follows:
(1) the new operator is used to create objects and call constructors.
(2) The new modifier is used to hide the inherited members of the base class members.
(3) The new constraint is used to restrict the types of parameters that may be used as type parameters in a generic declaration.
New operator
1. Used to create objects and call Constructors
Example: class_test myclass = new class_test ();
2. It is also used to call the default constructor for the value type.
For example, int Myint = new int ();
Myint is initialized to 0, which is the default value of int type. This statement is equivalent to: int Myint = 0;
3. The new operator cannot be overloaded.
4. If the new operator fails to allocate memory, it will cause an outofmemoryexception.
New Modifier
Use the new modifier to explicitly hide the members inherited from the base class. To hide an inherited Member, declare the member in the derived class with the same name and modify it with the new modifier.
See the following classes:
The following is a reference clip: 1 public class myclass 2 3 { 4 5 Public int X; 6 7 public void invoke (){} 8 9} 10 |
Using the invoke name in a derived class to declare a member will hide the invoke method in the base class, namely:
The following is a reference clip: 1 public class myderivedc: myclass 2 3 { 4 5 new public void invoke (){} 6 7} 8 |
However, because field X is not hidden by similar names, this field is not affected.
Hiding names by inheritance takes one of the following forms:
1. Introduce constants, designation, attributes, or types in the class or structure to hide all base class members with the same name.
2. introduce methods in a class or structure to hide attributes, fields, and types with the same name in the base class. It also hides all base class methods with the same signature.
3. The introduced class or structure indexer will hide all base class indexers with the same name.