MSDN says:
When used as a modifier, the new
keyword explicitly hides a member
inherited 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 the use of the
new modifier, the result is a warning.
If you use new to explicitly hide a
member, it suppresses this warning and
documents the fact that the derived
version is intended as a replacement.
Example:
class Base
{
int value;
virtual bool Foo()
{
value++;
}
}
class Derived : Base
{
int value;
override bool Foo()
{
value++;
}
}
Do I have to add new modifier to Derived.value declaration? What changes?
Since the
valuefield isprivate, it’s not accessible in the derived class. Thus, the declaration in the derived class does not really hide anything. You shouldn’t addnewto the declaration. If you do, nothing changes, except the compiler will warn you about usingnewincorrectly. If thevaluefield was accessible in the derived class (e.g. it waspublic), then you should have usednewto express your intention to hide the base member:Using
newwill silence that warning (it will have no other effect):You can’t declare a method as both
overrideandnew. They are mutually exclusive.