Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2241
  • Last Modified:

How to check if int is null in c#?

How to check if int is null in c#?
0
developer2012
Asked:
developer2012
2 Solutions
 
developer2012Author Commented:
How to check if int is null in c#?
0
 
Éric MoreauSenior .Net ConsultantCommented:
if your variable is declare as int?, you can use YourVariableName.HasValue
0
 
frankhelkCommented:
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.
0
 
Pratik MakwanaCommented:
I think you are looking for this.....

if(variable == null)
    Printf .....
else
    Printf.......
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now