First, in C #, the New keyword is used very frequently, there are 3 main functions:
A) used as an operator to create an object and call the constructor.
b) as a modifier.
c) To constrain the type of a parameter that may be used as a type parameter in a generic declaration.
The 1.new operator is used to create objects and invoke constructors.
1. For creating objects and calling constructors
Example: People p=new people ();
2. Also used to call the default constructor for value types
Example: int num= new int ();
Num is initialized to 0, which is the default value of type int. The effect of the statement is equal to: int myInt = 0;
3. You cannot reload the new operator.
4. If the new operator fails to allocate memory, it throws a OutOfMemoryException exception.
The 2.new modifier is used to hide an inherited member from a base class member.
Use the new modifier modifier explicitly to hide members inherited from the base class. To hide an inherited member, declare the member in the derived class with the same name and decorate it with the new modifier. When used as a modifier, the new keyword can hide a method of a base class in a derived class, or the method that uses a derived class is to invoke a method that is newly defined by the New keyword, not a method of the base class. It is also possible to hide the base class method without using the New keyword, and the compiler will see a warning that if you intentionally hide the method of the base class, use the new keyword modifier.
One of the things to note here is that the two keywords for new and override are mutually exclusive. Can not be used at the same time.
A 3.new constraint is used to constrain the type of a parameter that may be used as a type parameter in a generic declaration.
The new () constraint means that the passed-in class parameter must have an accessible parameterless (or default) constructor. This means that the class passed in will certainly be instantiated through new ().
If the class T has a new constraint, you can use the new T () method in the generic definition
The override keyword is primarily to provide a new implementation of a derived class for a base class method, overriding a base class method that must have the same signature as the override method, which cannot be used to override non-virtual and static methods, and the keyword that is used with it is virtual, abstract, Override. At the same time, the override method can not modify the accessibility of the virtual method, and the override method must have the same access modifier as the virtual method, and cannot be modified with the modifier new, static, virtual, or abstract Override method.
Here is a small demo showing the essential differences between new and override:
Class Program
{
static void Main (string[] args)
{
Contact Ct1 = new Class1 ();
Contact CT2 = new Class2 ();
Ct1.prinf ();
Ct2.prinf ();
}
}
Abstract public class Contact
{
public virtual void Prinf ()
{
Console.WriteLine ("This is a virtual method");
}
}
public class Class1:contact
{
public override void Prinf ()
{
Console.WriteLine ("This is the New method");
}
}
public class Class2:contact
{
Public new void Prinf ()
{
Console.WriteLine ("This is another new method");
}
}
Several uses of the New keyword for C #