开发者

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


  1. Object instantiation
  2. In anonymous types
  3. To signal that a member of the base class is being hidden.
  4. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜