Notifications
Clear all

Cannot convert null to 'int' because it is a non nullable value type  

   RSS

1

Below is my c# code,

int x = null;

I am trying to assign a null value to an integer variable and it is showing the following error: "Cannot Convert null to 'Int' because it is a non-nullable value type." Why?

This topic was modified 1 year ago by Raja
Topic Tags
C#
1 Answer
1

This is a common type of error we usually face while coding. There are two ways to solve this problem. 

Nullable<int> x = null;
int ? x = null;

The above are the two ways to convert a non-nullable value type to nullable value type in C#. A type is said to be nullable if it can be assigned a value or can be assigned null, which means the type has no value whatsoever. By default, all reference types, such as strings, are nullable, but all value types, such as Int32, are not.

Share:
Share this
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •