Check if integer == null
Solution 1
As stated, an int cannot be null. If a value is not set to it, then the default value I believe is zero. You can do a check for 0 if that is what you think it is being set to...
Otherwise if you are keen on avoiding nullable integers, you would have to turn it into a string and then perform the check.
String.IsNullOrEmpty(ID.ToString());
Which returns a boolean so you can stick it into an if statement.
Solution 2
An int
cannot be null. On the other hand a nullable int, int?
, can be. That being said the check that you want to do is meaningless.
Changing the type of ID
from int
to int?
, the check you do has a meaning and the compiler wouldn't complain about it.
Solution 3
you can use this ID.HasValue it return true when there is a value and false if null
Yannik
I am an IT-Trainee and love to code in C#, Javascript, and Java!
Updated on June 06, 2020Comments
-
Yannik almost 4 years
I want to check if my Listentry = null but I don't know how.
Note: ID is a Integer
Here is my Code
if(MeineGaeste[i].ID !=null) { i++; } else { MeinGast.ID = i++; test = false; }