A few days ago to the company interview, there is a question: Write C # in the New keyword of the three usages, after thinking and only came up with two usages, came back to check the next MSDN, there is a third use: used in a generic declaration to constrain the type of parameters that may be used as a type parameter, which is in the framework 2.0 in the definition of a generic row will be used, their support in C # 2.0 is only Cutong fur, no wonder that exhausted so many brain cells can not think of this third!
There are three ways to use:
in C #, the New keyword can be used as an operator, modifier, or constraint.
1) New operator: used to create objects and invoke constructors. This kind of people are more familiar with, there is nothing to say.
2) New modifier: When used as a modifier, the new keyword can explicitly hide members inherited from a base class.
3)
new constraint: Used to constrain the type of a parameter that may be used as a type parameter in a generic declaration. for the second usage look at the following example: Using system;namespace consoleapplication1{ public class basea { public int x = 1; public void invoke () { console.writeline (X.tostring ()); } public int truevalue { get { return x; } set { x = value; } } } Public class derivedb : basea { new public Int x = 2; new public void invoke () { console.writeline (X.tostring ()); } new public int TrueValue { get { return x; } set { x = value; } } } class Test { static void maIn (String[] args) { derivedb b = new derivedb (); b.invoke ();//Call Derivedb's Invoke method, output:2 console.writeline (B.x.tostring ());//The member of the output derivedb x value:2 BaseA a = b; a.invoke ();//Call Basea's Invoke method, output:1 a.truevalue = 3;//calls Basea's property TrueValue, Modify the value of member X for Basea console.writeline ( A.x.tostring ());//The value of the member X of the output Basea:3 Console.WriteLine (B.truevalue.tostring ());The value of the member X for the output derivedb is still: 1//is visible, to access the member variable, property, or method of the hidden base class, by styling the subclass as a parent class, then//after accessing the hidden member variable, property, or method through the base class. } }}
New A constraint specifies that any type parameter in a generic class declaration must have a public parameterless constructor. Take a look at the following example: Using system;using system.collections.generic; namespace consoleapplication2{ public class employee { private string name; private int id; public Employee () { name = "Temp"; id = 0; } public employee (string s, int i) { name = s; id = i; } public string Name { get { return name; } set { name = value; } } public int id { get { return id; } set { id = value; } } } class itemfactory< T> where t : new () { public t getnewitem () { return new t (); } } public class Test { public static void main () { ItemFactory<Employee> EmployeeFactory = new ItemFactory<Employee> (); ////here the compiler checks to see if the employee has a public parameterless constructor. //if not, there will be the employee must. have a public parameterless constructor error. console.writeline ("{0} ' ID is {1}. ", employeefactory.getnewitem (). Name, employeefactory.getnewitem (). ID); } }} See the explanation of new on MSDN new modifier (C # Reference)
When used as a modifier, thenew keyword can explicitly hide members that inherit from a base class. when you hide an inherited member, the derived version of the member replaces the base class version. Although you can hide members without using the new modifier, a warning is generated. If you explicitly hide a member by using new, this warning is canceled and the fact that you want to replace it with a derived version is logged.
To hide an inherited member, declare the member in the derived class with the same name, and use the new modifier to decorate the member. For example:
public class Basec
{
public int x;
public void Invoke () {}
}
public class Derivedc:basec
{
New public void Invoke () {}
}
In this example,Derivedc.invoke hides the basec.invoke. field x is not affected because it is not hidden by a field that resembles a name.
Hiding names by inheritance takes one of the following forms:
Introduces constants, designations, attributes, or types in a class or struct to hide all base class members with the same name.
Introduce methods in a class or struct to hide properties, fields, and types that have the same name in the base class. It also hides all base class methods with the same signature.
Introducing indexers in a class or struct hides all base class indexers with the same name.
Using both new and override on the same member is an error, because the meanings of the two modifiers are mutually exclusive. The new modifier creates a fresh member with the same name and turns the original member into a hidden one. the override modifier extends the implementation of the inherited member.
Using the new modifier in a declaration that does not hide an inherited member generates a warning.
Example
In this example, the base class basec and the derived class Derivedc use the same field name X, which hides the value of the inherited field. The example demonstrates the use of the new modifier. It also demonstrates how to use a fully qualified name to access hidden members of the base class.
public class Basec
{
public static int x = 55;
public static int y = 22;
}
public class Derivedc:basec
{
Hide field ' x '.
new public static int x = 100;
static void Main ()
{
Display the new value of x:
Console.WriteLine (x);
Display the hidden value of x:
Console.WriteLine (basec.x);
Display the Unhidden member Y:
Console.WriteLine (y);
}
}
/*
Output:
100
55
22
*/
In this example, a nested class hides a class with the same name in the base class. This example demonstrates how to use the new modifier to dismiss a warning message and how to use a fully qualified name to access a hidden class member.
public class Basec
{
public class Nestedc
{
public int x = 200;
public int y;
}
}
public class Derivedc:basec {//Nested type hiding the base type.
New public class Nestedc
{
public int x = 100;
public int y;
public int z;
}
static void Main ()
{
Creating an object from the overlapping class:
Nestedc C1 = new Nestedc ();
Creating an object from the hidden class:
BASEC.NESTEDC C2 = new Basec.nestedc ();
Console.WriteLine (c1.x);
Console.WriteLine (c2.x);
}
}
/*
Output:
100
200
*/
If you remove the new modifier, the program can still compile and run, but you receive the following warning:
The keyword new is required on ' myderivedc.x ' because it hides inherited member ' mybasec.x '.
Three ways to use the. NET (C #) New keyword