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 Nullable Warning Silenced by an Unsafe Cast

- 18.11.26 - ErcanOPAK

đź”§ C#: Fix a Nullable Warning Silenced by an Unsafe Cast

Nullable reference type analysis is a compile-time, flow-based warning system, and an explicit cast like (string)maybeNullValue is one of the operations the compiler treats as an assertion that you already know better — it suppresses the nullable warning at that line without inserting any actual runtime null check. Unlike the null-forgiving ! operator, which is specifically designed to be visible and greppable as an intentional override, a plain cast looks like an ordinary, unremarkable cast to a reviewer, hiding exactly the risk it introduces.

🔎 The Problem

#nullable enable
string? maybeName = LookupName(id);

// Looks like a routine cast; actually suppresses the nullable
// warning with zero runtime verification that maybeName isn't null:
string name = (string)maybeName;

Console.WriteLine(name.Length);
// NullReferenceException at runtime if LookupName returned null -
// the compiler gave no warning here at all

âś… Fix: Make Any Deliberate Override Visible and Searchable

  • Prefer the null-forgiving operator ! over a cast when you’ve manually verified a value can’t be null here — it communicates the same intent to the compiler while staying immediately visible and easy to find with a project-wide search.
  • Where the value’s nullability genuinely needs to be checked rather than merely asserted, use ArgumentNullException.ThrowIfNull or an explicit if (x is null) pattern so a real null actually throws a clear exception instead of flowing silently into later code.
  • During code review, treat an explicit reference-type cast the same way you’d treat a null-forgiving ! — as a claim that deserves a one-line justification, not as an unremarkable, self-evidently safe operation.

🚨 Why This Is Dangerous

  • A cast that silences a nullable warning doesn’t just suppress it for the current code — it keeps suppressing it after later edits change what’s being assigned, so a value that was genuinely non-null when the cast was written can become nullable again without the compiler ever re-flagging that line.

The null-forgiving operator says ‘I checked this’ out loud; a cast says the same thing in a whisper a reviewer has no reason to listen for.

— C# Best Practice

Related posts:

C#: Use Object Initializers for Clean Object Creation

C#: Use Object Initializers for Clean Object Creation

C#: Use Async Streams for Asynchronous Data Sequences

C#: Use Async Streams for Asynchronous Data Sequences

ASP.NET Core “Request Body Already Read” — Enable Buffering

ASP.NET Core “Request Body Already Read” — Enable Buffering

Views: 1

Post navigation

Windows 11: Fix a Pen’s Input Lagging in Only One Specific App
Asp.Net Core: Fix a Test Factory Hitting the Real Database by Accident

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