In C#, the new keyword can be used as an operator, modifier, or constraint.
new operator
Used to create objects and call constructors.
new modifier
Used to hide inherited members from base class members.
new constraints
Used to constrain the types of parameters that may be used as type parameters in a generic declaration.
new modifier (C# reference)
When used as a modifier, the new keyword can explicitly hide members inherited from the base class. Hide inherited members means that the derived version of that member will replace the base class version. Hiding members without using the new modifier is allowed, but a warning is generated. Using new to explicitly hide members cancels this warning and records the fact that it replaces it with a derived version.
To hide an inherited member, declare the member in the derived class with the same name and modify the member with the new modifier.
new operator (C# reference)
1. Used to create objects and call constructors. For example:
Class1 o = new Class1();
2. Also used to call the default constructor for value types
Example: int myInt = new int();
myInt is initialized to 0, which is the default value of type int. The effect of 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 throw an OutOfMemoryException exception
new constraints (C# reference)
The new constraint specifies that any type parameter in a generic class declaration must have a public parameterless constructor. When a generic class creates a new instance of a type, apply this constraint to the type parameter, as shown in the following example:
class ItemFactory<T> where T : new() { public T GetNewItem() { return new T(); } }
Hide names by inheriting them in one of the following forms:
1. Introduce constants, specifications, attributes, or types in a 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. At the same time, all base class methods with the same signature are also hidden.
3. Introducing indexers in a class or structure will hide all base class indexers with the same name.
4. It is wrong to use new and override on the same member.
Note: Using the new modifier in a declaration that does not hide inherited members will generate a warning.
Example
In this example, the nested class MyClass hides classes with the same name in the base class. This example not only illustrates how to use a fully qualified name to access hidden class members, but also illustrates how to use the new modifier to eliminate warning messages.
using System; public class MyBaseC { public class MyClass { public int x = 200; public int y; } } public class MyDerivedC : MyBaseC { new public class MyClass // nested type hiding the base type members { public int x = 100; public int y; public int z; } public static void Main() { // Creating object from the overlapping class: MyClass S1 = new MyClass(); // Creating object from the hidden class: S2 = new (); (); (); } } Output 100 200
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!