Sunday 8 January 2012

New Keyword in c#


New - This keyword may be used as a modifier and as an operator. When used as an operator, it creates an object on a heap to invoke constructors. When used an a modifier, it hides an inherited member from the base class member.

As an operator, it can be used to create an object and then to invoke the constructor of the class. See example below.
Example
SomeClass objSomeClass = new SomeClass(); //Creating a class object and invoking its constructor

float amount = new float(); //Creating an object of the type, and invoking its constructor
As a modifier, it is used to explicitly hide a member from the base class. See example.
Example
public class MamaClass
{
public void SomeMethod() { ... }
}

public class BabyClass : MamaClass
{
new public void SomeMethod() { .... }
}

No comments:

Post a Comment