Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix a Primary Constructor Parameter Shadowed by a Field

- 29.11.26 - ErcanOPAK

🔧 Fix a Primary Constructor Parameter Shadowed by a Field

A C# 12 primary constructor parameter is only captured into a compiler-generated backing field when something inside the class actually reads it from an instance member; until then, it behaves like an ordinary parameter in scope. Declare an explicit field with the exact same name as that parameter, and every reference to that name inside your instance methods resolves to the field you declared, not to the primary constructor’s captured value — the two coexist, but whichever one a given line of code happens to see depends on which declaration the compiler resolves first, which is easy to get backwards.

🔎 The Problem

public class Customer(string name)
{
    private string name = name.Trim(); // explicit field, same name as
                                         // the primary constructor parameter

    public void Rename(string newName)
    {
        name = newName;
        // Assigns the explicit FIELD - fine here, this is what you want.
        // But anywhere else in the class that references "name" is also
        // reading/writing that SAME field, not a separately captured
        // primary-constructor value - there's only ever one "name" in
        // scope once the field is declared, which can be surprising if
        // you expected the raw constructor argument to still be reachable
    }
}

✅ Fix: A Primary Constructor Parameter Shadowed by a Field

  • Give the explicit field a distinct name (the common convention is a leading underscore, `_name`) whenever you need both the primary constructor’s raw parameter and a transformed or validated version stored separately.
  • If you only need the transformed value and never the raw parameter again, that’s exactly what the shadowing field pattern above is for — just name it deliberately so a future reader knows it’s intentional, not an accident of matching names.
  • Search for any primary constructor parameter name that’s reused as a field or property name elsewhere in the same class before assuming the parameter is still directly reachable everywhere in the class body.

🚨 Why This Is Dangerous

  • Code review easily misses this, since both the parameter and the field share a name and the type system never complains — the only sign something’s off is behavior that doesn’t match what the constructor argument was supposed to do.

A primary constructor parameter only has one chance to be captured as-is — declare a field with the same name, and from then on, the field is the only one anybody’s talking to.

— C# Best Practice

Related posts:

C#: Implement Rate Limiting with Async/Await

C#: Implement Rate Limiting with Async/Await

How to add default value for Entity Framework migrations for DateTime and Bool

How to add default value for Entity Framework migrations for DateTime and Bool

C#: Fix an Explicit Conversion Operator Silently Never Getting Called Because the Compiler Chose Box...

C#: Fix an Explicit Conversion Operator Silently Never Getting Called Because the Compiler Chose Box...

Views: 5
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Asp.Net Core: Fix a Rate Limiter Resetting Its Window Too Early
Windows 11: Fix Widgets Panel Showing a Blank White Screen

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 996 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 995 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 866 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com