How to properly idiomatic avoid CS9107 when passing primary constructor parameters to base
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
CS9107 appears when a primary constructor parameter is passed to a base constructor and also captured by the derived type. The warning is not about syntax alone; it is about duplicated state and unclear ownership. The idiomatic fix is usually to decide which class should store the value and then use that one source of truth.
What Triggers CS9107
This pattern causes the warning:
The same name value is used twice:
- passed into the base constructor
- captured again by the derived class because
Printreferences it
The compiler warns because both types may end up retaining the same value, which is often redundant.
Preferred Fix: Let the Base Own the Value
If the value is part of the base class contract, store it in the base and read it from there.
This is the most idiomatic solution in typical inheritance hierarchies. The base receives the constructor parameter, stores it once, and the derived type consumes the inherited member.
Another Fix: Stop Capturing It in the Derived Type
Sometimes the derived type does not need the parameter directly at all. In that case, do not reference the primary constructor parameter from the body.
Here, UserHandler no longer captures logger. The base owns it, which removes the warning and improves clarity.
When Duplicate Storage Is Intentional
Occasionally, both classes need related but distinct versions of the same constructor input. That should be explicit, not accidental.
This still stores two values, but now the duplication is purposeful. One member holds the original input and the other holds the normalized form. The names make the design intent obvious.
Dependency Injection Case
CS9107 shows up often with dependency injection because service parameters are easy to pass to a base class and then accidentally reuse in the derived class.
Warning-prone version:
Better version:
The fix is usually small, but it improves the ownership model a lot.
Design Rule of Thumb
When CS9107 appears, ask one question first: which type should conceptually own this value?
Use this rule:
- if the value defines shared base behavior, store it in the base
- if only the derived class needs it, do not pass it through the base unless required
- if both need separate representations, name those representations explicitly
That approach leads to cleaner inheritance and makes the warning useful instead of annoying.
Common Pitfalls
- Trying to silence CS9107 without clarifying which class actually owns the state.
- Keeping both the primary constructor parameter and a semantically identical base property.
- Duplicating injected services across base and derived classes for no benefit.
- Hiding the base member with another field or property that means the same thing.
- Treating the warning as harmless even when it points to muddled class design.
Summary
- CS9107 happens when a primary constructor parameter is captured in the derived type and passed to the base.
- The idiomatic fix is usually to let the base store the value once and use the base member.
- If duplicate storage is intentional, make the distinction explicit in member names.
- The warning is mainly about state ownership and inheritance clarity.
- Clean design usually removes the warning naturally without hacks.

