Detailed explanation of several usages of new in C

  • 2021-12-11 08:40:16
  • OfStack

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 Constraint

Used to constrain the types of parameters that may be used as type parameters in generic declarations.

new modifier (C # reference)

When used as a modifier, the new keyword can explicitly hide members inherited from the base class. Hiding an inherited member means that a derived version of the member will replace the base class version. Hiding members without the new modifier is allowed, but a warning is generated. Using new to explicitly hide a member cancels this warning and records the fact that it is replaced by a derived version.

To hide an inherited member, declare the member in a 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 a value type

Example: int myInt = new int ();

myInt is initialized to 0, which is the default value for int type. 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 throws 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(); 
} 
}

Hiding names by inheritance takes one of the following forms:

1. Introduces constants, specifiers, properties, 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 properties, fields, and types with the same name in the base class. It also hides all base class methods with the same signature.

3. Introducing an indexer into a class or structure hides all base class indexers with the same name.

4. Using both new and override on the same 1 member is an error.

Note: Using the new modifier in declarations that do not hide inherited members generates warnings.

Example

In this example, the nested class MyClass hides classes with the same name in the base class. This example not only shows how to use fully qualified names to access hidden class members, but also shows 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: 
    MyBaseC.MyClass S2 = new MyBaseC.MyClass(); 
         Console.WriteLine(S1.x); 
         Console.WriteLine(S2.x); 
         } 
         } 

 Output 
        100
        200

Related articles: