C# Error CS0037 – Cannot convert null to ‘type’ because it is a non-nullable value type

C# Compiler Error Message

CS0037 -Cannot convert null to ‘type’ because it is a non-nullable value type

Reason for the Error

This is one of the simplest and straight forward errors that you will receive in C#. The error indicates that it cannot assign null to a value type. Take the below code as an example.

public class DeveloperPublish
{
    public static void Main()
    {
        int input = null;    
    }
}

We are assigning null value to a variable of type int which results with this error.

Error CS0037 Cannot convert null to ‘int’ because it is a non-nullable value type ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs

Solution

You can easily fix this error by changing the value type as nullable type as shown below.

public class DeveloperPublish
{
    public static void Main()
    {
        int ? input = null;    
    }
}
Copy link
Powered by Social Snap