Skip to main content

How to Resolve Error "CS0037: Cannot convert null to 'type' because it is a non-nullable value type" in C#

The Compiler Error CS0037 is a type system error. For example, the message could be: "Cannot convert null to 'int' (or other type) because it is a non-nullable value type".

In C#, types are divided into Reference Types (classes, strings, interfaces) and Value Types (integers, booleans, structs, dates). Reference types can point to null (empty/nothing). Value types, however, hold their data directly and must always have a valid value. You cannot have an integer that is "nothing"; it must be a number.

This guide explains how to handle situations where you need to represent missing data using value types.

Understanding Value Types​

Primitive numeric types (int, double, byte), logical types (bool), and complex structures (DateTime, Guid, custom struct) are Value Types.

  • A string (Reference Type) can be null.
  • An int (Value Type) cannot be null. It defaults to 0.

CS0037 occurs when you force null into a container that isn't designed to hold it.

Scenario 1: Basic Primitives (int, bool, double)​

The most common occurrence is trying to initialize a number or boolean with null, perhaps to indicate that the value hasn't been set yet.

Example of Mistake​

public void ProcessId()
{
// ⛔️ Error CS0037: Cannot convert null to 'int' because it is a non-nullable value type
int id = null;

// ⛔️ Error CS0037: Same issue with boolean
bool isComplete = null;
}

Solution 1: Use a Default Value​

If the variable must stay a standard int, assign a sensible default value (like -1 or 0) to represent "not set".

public void ProcessId()
{
// ✅ Correct: Use 0 or -1 to represent 'empty'
int id = 0;
bool isComplete = false;
}

Solution 2: Use Nullable Types (T?)​

If "no value" is a valid state for your logic (e.g., database results), change the type to a Nullable type by adding a question mark ? after the type name.

public void ProcessId()
{
// ✅ Correct: 'int?' (shorthand for Nullable<int>) can hold numbers OR null
int? id = null;

if (id.HasValue)
{
Console.WriteLine(id.Value);
}
else
{
Console.WriteLine("ID is missing");
}
}
note

int? is actually syntactic sugar for the struct System.Nullable<int>. This wrapper allows value types to simulate nullability.

Scenario 2: DateTime and Structs​

DateTime is a struct, not a class. Beginners often assume it works like a string and try to assign null to clear a date.

Example of Mistake​

public class UserProfile
{
public string Name { get; set; }

// ⛔️ Error CS0037: DateTime is a struct
public DateTime LastLoginDate { get; set; } = null;
}

Solution​

Use DateTime? or DateTime.MinValue.

public class UserProfile
{
public string Name { get; set; }

// ✅ Correct: Make it nullable
public DateTime? LastLoginDate { get; set; } = null;

// OR

// ✅ Correct: Use the type's default minimum value
public DateTime CreatedDate { get; set; } = DateTime.MinValue;
}

Scenario 3: The Conditional (Ternary) Operator​

This is a subtle variation of the error. When using the ternary operator condition ? A : B, both A and B must be convertible to a common type. If one side is a value type (int) and the other is null, the compiler may fail to infer that you intended a nullable result.

Example of Mistake​

bool hasValue = false;

// ⛔️ Error CS0037 (or implicit conversion error):
// The compiler sees '10' (int) and 'null'.
// It tries to make the result an 'int', but 'null' cannot be an 'int'.
var result = hasValue ? 10 : null;

Solution: Cast to Nullable​

You must explicitly tell the compiler that the 10 is actually an int?.

bool hasValue = false;

// ✅ Correct: Cast the value side to 'int?'
// Now both sides are compatible (int? and null).
int? result = hasValue ? (int?)10 : null;
tip

From C# 9.0 onwards, target-typed conditional expressions often handle this better if the variable type is explicit: int? result = hasValue ? 10 : null; works. However, var result = ... will still struggle without the cast.

Conclusion​

CS0037 reminds you that Value Types require actual values.

  1. Identify the Type: Is it int, bool, double, DateTime, or Guid?
  2. Decide Intent:
    • Does "null" mean "Zero" or "False"? Use a default value (0, false).
    • Does "null" mean "Unknown/Missing"? Use a Nullable Type (int?, bool?).
  3. Check Ternaries: If returning null in a conditional, ensure the other branch is cast to a nullable type.