C#
SQL Server
IsNull
programming
database

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:

sql
SELECT ISNULL(EmailAddress, '[email protected]')

In C#:

csharp
1string? emailAddress = null;
2string displayEmail = emailAddress ?? "[email protected]";
3
4Console.WriteLine(displayEmail);

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?.

csharp
1int? quantity = null;
2int safeQuantity = quantity ?? 0;
3
4decimal? discount = 12.5m;
5decimal appliedDiscount = discount ?? 0m;
6
7Console.WriteLine(safeQuantity);
8Console.WriteLine(appliedDiscount);

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.

csharp
1int? points = null;
2int a = points.GetValueOrDefault();
3int b = points.GetValueOrDefault(50);
4
5Console.WriteLine(a); // 0
6Console.WriteLine(b); // 50

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 ??.

csharp
1public sealed class Customer
2{
3    public string? City { get; set; }
4}
5
6Customer? customer = null;
7string city = customer?.City ?? "Unknown city";
8
9Console.WriteLine(city);

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.

csharp
1object dbValue = DBNull.Value;
2
3string name = dbValue == DBNull.Value
4    ? "No name"
5    : (string)dbValue;
6
7Console.WriteLine(name);

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 DBNull handling 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 ISNULL is the null-coalescing operator ??.
  • Use ?? for both reference types and nullable value types.
  • Use GetValueOrDefault when working specifically with Nullable<T>.
  • Combine ?. and ?? for safe navigation with a fallback value.
  • Handle DBNull.Value explicitly when reading database values through older APIs.

Course illustration
Course illustration

All Rights Reserved.