new keyword in property declaration in c
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, the new keyword on a property declaration means "this member hides an inherited member with the same name." It does not override the base property, and it does not change runtime polymorphism the way override does. That distinction is the entire point of the feature, and misunderstanding it leads to subtle bugs.
What new Means on a Property
Suppose a base class defines a property:
Now a derived class declares another property with the same name:
The new keyword tells the compiler that this is intentional member hiding. The Dog.Name property hides Animal.Name when the reference type is Dog.
That is not the same as replacing the base member globally.
Hiding Versus Overriding
The easiest way to see the difference is through reference types:
With new, these references can resolve to different properties because lookup depends on the compile-time type of the variable:
- '
animalRef.NameusesAnimal.Name' - '
dogRef.NameusesDog.Name'
That behavior surprises people who expected polymorphism.
If you want polymorphic behavior, the base property must be virtual, and the derived property must use override:
Now both references use the overridden property implementation.
Why the Compiler Warns Without new
If you declare a same-named member in the derived class without new, C# gives a warning. The compiler is telling you:
- either you meant to override and forgot
- or you meant to hide and should say so explicitly
Example:
This compiles with a warning because the language wants you to acknowledge the hiding behavior intentionally.
Adding new does not change runtime magic. It mainly makes the hiding explicit and silences the warning.
When Hiding a Property Makes Sense
Using new is relatively uncommon, but it can make sense when:
- the base member is not
virtual - you cannot modify the base type
- the derived API needs a same-named member with different semantics
For example, a framework base class may expose a property that you want to replace at the derived API surface for convenience, even though true overriding is unavailable.
Still, this is usually a sign to slow down and ask whether the API is becoming confusing.
Why new Can Be Dangerous
Because hidden members depend on reference type, the same object can appear to have different values depending on how it is referenced.
Example:
These are two distinct properties stored separately. That is often more confusing than people expect.
Property Hiding Is Not Limited to get and set
The same hiding behavior applies regardless of whether the property is:
- auto-implemented
- computed
- read-only
- write-only
The key fact is the member name and inheritance relationship, not the property body style.
So this also hides:
The property form changes, but the hiding rule does not.
Prefer override When the Design Calls for Polymorphism
If the conceptual intent is "derived types should customize the base property," then virtual plus override is almost always the better design.
Use new only when you genuinely want name hiding. That is a narrower and more specialized tool.
A good rule of thumb is:
- use
overridefor substitutability - use
newfor explicit hiding when override is not the right model
If you cannot explain why the member should be hidden rather than overridden, new is probably the wrong answer.
Common Pitfalls
The biggest mistake is using new when the real goal was polymorphic behavior. Hidden members are selected by reference type, not by runtime dispatch.
Another issue is forgetting that the base and derived properties can hold different values. That can create debugging confusion because the object appears inconsistent depending on how it is accessed.
Developers also sometimes add new just to silence the compiler warning without understanding the API consequence. That fixes the warning, not the design.
Finally, if the base property is meant to be customizable, prefer virtual and override instead of hiding. The resulting code is usually clearer and safer.
Summary
- '
newon a property means the derived property hides an inherited property with the same name.' - Hiding is not overriding, and it does not use runtime polymorphism.
- Hidden member lookup depends on the compile-time type of the reference.
- Use
overridewhen you want polymorphic behavior andnewonly when you intentionally want hiding. - If a hidden property creates confusion, the API design is usually the real problem.
Related reading
- ''Newtonsoft.Json'' already has a dependency defined for ''Microsoft.CSharp''
- No AppDomains in .NET Core Why?
- No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization
- No ConcurrentListT in .Net 4.0?
- No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient
- No output to console from a WPF application?
- No overload for method 'ToString takes 1 arguments when casting date
- node.js vs. asp.net async pages

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.