What are the different purpose of "new" is available in C#
Can any one explain in th开发者_JAVA技巧e detail(with example) the different purpose of the "new" in C#.
You have:
- new operator:
Used to create objects and invoke constructors
- new modifier
When used as a modifier, the new keyword explicitly hides a member inherited from a base class
- new constraint
The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor
- Object instantiation
- In anonymous types
- To signal that a member of the base class is being hidden.
- As a constraint
About 3 (from MSDN):
public class BaseC
{
public int x;
public void Invoke() { }
}
public class DerivedC : BaseC
{
new public void Invoke() { }
}
The keyowrd is not necessary but should be used to make it clear that the base-class constructor is being hidden.
精彩评论