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;
}
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;
}
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.
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.
you can use this ID.HasValue it return true when there is a value and false if null
As a normal int
type is a value type, it can not be null
.
You can use the nullable type int?
for the property ID
which is basically a struct, that allows null
or any int
value. See MSDN or this question or this question for additional documentation.
If it is not possible to change the type of the ID
property, a workaround might be to choose an integer as marker value which is guaranteed not appear in your model (for example Int32.MinValue
, as most generated IDs seem to be positive integers). Don't do this if you don't have to, as this will most likely lead to errors. I would a least recommend to throw an exception if you serialize models with that marker value.