C equivalent of the IsNull function in SQL Server
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, the closest equivalent to SQL Server ISNULL(value, replacement) is usually the null-coalescing operator ??. It returns the left-hand value when that value is not null, otherwise it returns the fallback value on the right.
That is the most direct mental mapping for developers moving from SQL to C#, but the full story includes nullable value types, DBNull, and the difference between database null semantics and in-memory object semantics.
The Direct Equivalent: ??
In SQL:
In C#:
If emailAddress is non-null, displayEmail gets that value. Otherwise it gets the fallback string.
Nullable Value Types
The same operator works with nullable value types such as int?, decimal?, or DateTime?.
This is a direct replacement pattern for SQL expressions such as ISNULL(Quantity, 0).
GetValueOrDefault for Nullable Structs
For nullable value types, GetValueOrDefault is another option.
This works only on Nullable<T> values, not on reference types such as string.
Null-Conditional and Null-Coalescing Together
In real C# code, the most useful pattern often combines the null-conditional operator ?. with ??.
This safely navigates the object graph and then applies a fallback.
null Versus DBNull
When data comes from ADO.NET, you may encounter DBNull.Value. That is not the same as C# null.
This matters when reading rows from DataTable, SqlDataReader, or older data-access APIs. ?? only checks for actual null, not database sentinel values wrapped in objects.
How the SQL and C# Worlds Differ
SQL ISNULL operates inside a query over database values. C# null handling operates in application memory over objects and nullable structs. The syntax looks similar, but the execution environment and type rules are different.
For example, SQL also has three-valued logic around NULL, while C# boolean expressions do not behave the same way unless you are specifically using nullable booleans.
So the best translation is usually conceptual rather than literal:
- SQL
ISNULL(col, fallback)maps to C#value ?? fallback - SQL nulls from a reader may need
DBNullhandling first - Nested property access often uses
?.and??together
Common Pitfalls
One common mistake is assuming ?? handles empty strings. It does not. An empty string is a real value, not null. If you want to treat empty text as missing, use string.IsNullOrEmpty or string.IsNullOrWhiteSpace.
Another problem is forgetting about DBNull.Value when working with older data-access code. A value may come back as a boxed database null rather than a C# null reference.
Developers also sometimes overuse GetValueOrDefault when ?? is clearer. For most application code, ?? is easier to read.
Finally, remember that nullable reference types in modern C# improve warnings at compile time, but they do not remove the need for runtime null handling.
Summary
- The closest C# equivalent to SQL Server
ISNULLis the null-coalescing operator??. - Use
??for both reference types and nullable value types. - Use
GetValueOrDefaultwhen working specifically withNullable<T>. - Combine
?.and??for safe navigation with a fallback value. - Handle
DBNull.Valueexplicitly when reading database values through older APIs.

