Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Fix a Struct’s Default Constructor Skipping Field Defaults

- 18.11.26 - ErcanOPAK

🔧 C#: Fix a Struct’s Default Constructor Skipping Field Defaults

C# 10 allows a struct to define its own parameterless constructor, but that constructor only runs when code explicitly calls new MyStruct(). Both default(MyStruct) and array allocation like new MyStruct[5] bypass it entirely and zero-initialize the memory directly, which means any field default or setup logic written inside that constructor simply never runs for a struct that arrives through either of those two very common paths.

🔎 The Problem

public struct Money
{
    public string CurrencyCode;
    public Money() { CurrencyCode = "USD"; }
}

var a = new Money();          // CurrencyCode == "USD" - constructor ran
var b = default(Money);       // CurrencyCode == null  - constructor skipped
var c = new Money[3];         // every element's CurrencyCode == null too

✅ Fix: Treat the Zeroed State as a Real Case, Not an Edge Case

  • Don’t rely on the parameterless constructor to guarantee an invariant that must hold everywhere; default and array allocation both skip it, and plenty of generic code (including some collections and serializers) produces structs exactly that way.
  • Where a field’s zero/null value isn’t a safe default, read it defensively at the point of use (treating null or zero as “not yet set” and resolving it there) instead of assuming the constructor already handled it.
  • If a type genuinely needs a parameterless-constructor guarantee that holds no matter how it’s created, that’s a sign it should be a class, not a struct — classes don’t have a zero-initialized default state that bypasses their constructor the way structs do.

🚨 Why This Is Dangerous

  • Code that works perfectly in every explicit new Money() call site and only fails once a Money[] array or a generic default(T) path is involved can pass all of its direct unit tests while still shipping a real, reachable bug.

A struct’s parameterless constructor is an invitation, not a guarantee — default and array allocation are both perfectly entitled to decline it.

— C# Best Practice

Related posts:

C#: Use Method Overloading for Multiple Signatures

C#: Use Method Overloading for Multiple Signatures

How to split string and get first (or n-th) value only

How to split string and get first (or n-th) value only

C# Foreach vs For Performance Difference

C# Foreach vs For Performance Difference

Views: 2

Post navigation

The AI Prompt That Rewrites a Callback Chain Into Async/Await
Ajax: Fix a Long-Poll Reconnecting Before the Server Replies

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

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