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 Required Member Satisfied by the Wrong Initializer

- 28.11.26 - ErcanOPAK

🔧 Fix a Required Member Satisfied by the Wrong Initializer

A constructor marked with [SetsRequiredMembers] is a promise to the compiler that it assigns every required member the type declares – but the compiler never actually checks that the constructor’s body keeps that promise. It only checks that call sites using the plain object-initializer syntax set every required member; a constructor carrying the attribute gets a free pass to skip one entirely, and the required property it never touches is silently left at its type’s default, null for a reference type, instead of producing the compile error a plain initializer omitting it would.

🔎 The Problem

public class ShippingLabel
{
    public required string RecipientName { get; init; }
    public required string Address { get; init; }
    public string? TrackingNumber { get; init; }

    public ShippingLabel() { }

    [SetsRequiredMembers]
    public ShippingLabel(string recipientName)
    {
        RecipientName = recipientName;
        // Address is required but never assigned here - the compiler
        // takes [SetsRequiredMembers] as a promise and does not check
        // that every required member actually got set inside this body
    }
}

var label = new ShippingLabel("Ayse Yilmaz"); // compiles cleanly
Console.WriteLine(label.Address.Length); // NullReferenceException at
                                          // runtime - Address was never
                                          // actually required here

✅ Fix: Don’t Trust SetsRequiredMembers Blindly

  • Treat [SetsRequiredMembers] as an unchecked promise rather than a verified guarantee – manually review every constructor carrying that attribute to confirm it genuinely assigns every required member the type declares, each time a required member is added or removed.
  • Prefer the plain, attribute-free object initializer syntax for call sites wherever possible, since that path is fully compiler-checked against every required member with no way to opt out.
  • Add a unit test that constructs the type through each [SetsRequiredMembers] constructor and asserts every required member has a non-default value afterward, so a future edit that adds a required property without updating an existing constructor fails a test immediately instead of surfacing as a null reference downstream.

🚨 Why This Is Dangerous

The entire point of required is to turn a missing-value bug into a compile error instead of a runtime one – but SetsRequiredMembers creates exactly one path back to the old behavior, and because it still compiles without warning, a reviewer has no compiler signal telling them to double-check that particular constructor’s body by hand.

required only protects the call sites the compiler actually checks – and SetsRequiredMembers is the one place it was explicitly told to stop checking.

— C# Best Practice

Related posts:

Pattern Matching for Cleaner Logic

Pattern Matching for Cleaner Logic

C# Collection Expressions: Write Cleaner Code with `[]` Syntax

C# Collection Expressions: Write Cleaner Code with `[]` Syntax

C#: Use LINQ to Query Collections Efficiently

C#: Use LINQ to Query Collections Efficiently

Views: 6
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Windows 11: Fix Quick Settings Volume Lagging Behind Audio
Git: Fix a Filter-Repo Run Orphaning an Old Tag Pointer

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 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 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# 868 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 @ 631 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 562 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