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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...