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;    
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...