Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if integer == null [closed]

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;
}
like image 756
Yannik Avatar asked Nov 27 '22 01:11

Yannik


2 Answers

As stated, an int cannot be null. If a value is not set to it, then the default value 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.

like image 75
averroes Avatar answered Dec 23 '22 19:12

averroes


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.

like image 44
Christos Avatar answered Dec 23 '22 20:12

Christos