gogoWebsite

Programming language introduces several usages of new in C#

Updated to 2 days ago

Programming language introduces several usages of new in C#

In C#, the new keyword can be used asOperators, modifier or constraint.

new operator

ForCreate an objectand callConstructor

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. NoReloadnew operator.

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, specify, andpropertyOr type hides 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. Introduce the class or structureindexThe user 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 access hidden class members using a fully qualified name, 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 a detailed introduction to the usage of new in C#. For more information, please pay attention to other related articles on php Chinese!

Source of the article: /faq/

Baidu Netdisk search
Awa Education
Composition brother
Search the code