This article mainly introduces the use of New in C #, and the difference with override, the need for friends can refer to the following
There are three ways to use New in C #:
(1) New is an operator that is used to create objects and invoke constructors. such as Class1=new Class1 (); You can also call the default constructor for a value type, such as int a=new int (), at which time a=0.
(2) New is a modifier that hides the inherited members of a base class member. Override cannot hide inherited members that accumulate members. Such as:
Using system;using system.collections.generic;using system.linq;using system.text;namespace A{ class Program { Abstract public class Test {public virtual void Prinf () { Console.WriteLine ("Abstract Printf ... ");} public class Class1:test {public override void Prinf () { Console.WriteLine ("Class one override Printf ... ");} public class Class2:test {public new void Prinf () { Console.WriteLine ("Class-New Printf ..."); } } static void Main (string[] args) { Class1 cls1 = new Class1 (); Class2 cls2 = new Class2 (); Cls1. Prinf (); Cls2. Prinf (); (Test) CLS1). Prinf (); (Test) CLS2). Prinf (); Console.ReadLine ();}}}
The result of the operation shows that the new modifier hides the overriding method, invoking the method of the base class by turning the derived object into a base class object. Override, however, is a complete rewrite of the method, even if it is called to the base class object, which is also the overridden method of the derived class.
That is: New hides the base class method, override overrides the base class method.
(3) New is a constraint. A constraint that adds a type to a generic type.
Using system;using system.collections.generic;using system.linq;using system.text;namespace A{ class Test<T > where T:new ()//Defines the constraint of type T, which means that the T type must have a constructor without parameters {public T GetItem () { return new T ();//If you do not add new () constraint, compile error: Variable type ' T ' does not have a new () constraint, so an instance of the type cannot be created //think about it, the T type does not know that the compiler does not know how much space is allocated, so it is implemented by reflection technology } } class Tclass { private int A; Public Tclass ()//If no parameterless constructor is added, compile error: Tclass must be a non-abstract type with a common parameterless constructor to be used as the parameter "T" {} in the generic type or method "A.test<t>" Public tclass (int a) { THIS.A = A; } } Class program { static void Main (string[] args) { test<tclass> Test = new test<tclass> (); Console.ReadLine ();}}}