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 Compiling Without a Setter Call

- 24.11.26 - ErcanOPAK

🔧 C#: Fix a Required Member Compiling Without a Setter Call

The required modifier on a property is enforced entirely by the compiler at the call site of an object initializer, verifying that every required member appears somewhere in the braces, with no runtime enforcement of its own. A constructor marked with SetsRequiredMembers tells the compiler to trust that the constructor has already satisfied every required member internally, suppressing the initializer check entirely for any caller who uses it, so if the constructor’s body doesn’t actually set every required property, the object is constructed successfully with those properties left at their default values and nothing anywhere catches the omission.

🔎 The Problem

public class Order
{
    public required string CustomerId { get; init; }
    public required string ShippingAddress { get; init; }

    [SetsRequiredMembers]
    public Order(string customerId)
    {
        CustomerId = customerId;
        // ShippingAddress never set here - but
        // [SetsRequiredMembers] tells the compiler to trust
        // this constructor satisfied every required member,
        // so `new Order("cust-1")` compiles cleanly and
        // ShippingAddress is silently null at runtime
    }
}

✅ Fix: Treat the Attribute as an Unverified Promise

  • Treat [SetsRequiredMembers] as a manual promise the compiler can’t verify, and set every single required property inside that constructor’s body, since the attribute only turns off the compiler’s own check rather than confirming anything was actually assigned.
  • Add a unit test that constructs the object via the attributed constructor and asserts every required property is non-default, specifically because this is exactly the gap no compiler warning will ever catch.
  • Prefer a plain object initializer without [SetsRequiredMembers] wherever possible, reserving the attribute only for constructors that genuinely need to set required members from internal logic rather than from caller-supplied arguments.

🚨 Why This Is Dangerous

  • A required property exists specifically to turn a missing value into a compile-time error instead of a runtime null-reference exception, and SetsRequiredMembers can quietly reopen exactly that hole for any constructor that doesn’t actually keep its promise.

required moves a null check from runtime to compile time, but only for callers using the initializer — a constructor marked SetsRequiredMembers is a promise the compiler stops checking.

— C# Best Practice

Related posts:

C#: Use required Modifier to Enforce Property Initialization

C#: Use required Modifier to Enforce Property Initialization

Why Dictionary Suddenly Becomes Slow

Why Dictionary Suddenly Becomes Slow

C#: Use ValueTuple for Lightweight Multiple Return Values

C#: Use ValueTuple for Lightweight Multiple Return Values

Views: 8
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

The AI Prompt That Turns a Vet Bill Into an Insurance Claim Draft
WordPress: Fix Media Thumbnails Regenerating at the Wrong Size

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 @ 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