How to check if int is null in c#?

How to check if int is null in c#?
Who is Participating?
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
if your variable is declare as int?, you can use YourVariableName.HasValue
developer2012Author Commented:
How to check if int is null in c#?
frankhelkConnect With a Mentor Commented:
Hmmm - as far as I know, any integral data type like int, float, double, etc. is not really an object, even while it could be treated as one with some weird cast (even while I don't know why one would like to do that besides of curiosity).

Therefore an int variable can have a value of 0, but could not be null (in the meaning of an unitialized object pointer). When it is created, it is initialized to a value of 0 by the compiler, but good practice would be to initialize it prior to usage of the value.

Under that aspects, checking an int variable for 0 is common practice, but checking it for null is quite senseless.

@Éric Moreau:
int? is a special type that explicity allows null as value. See here for explanation of that specialty. The type int itself definitely doesn't allow null.
Pratik MakwanaData AnalystCommented:
I think you are looking for this.....

if(variable == null)
    Printf .....
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.