Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to check if int is null in c#?

Posted on 2014-10-08
4
Medium Priority
?
2,102 Views
Last Modified: 2014-10-09
How to check if int is null in c#?
0
Comment
Question by:developer2012
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Author Comment

by:developer2012
ID: 40369411
How to check if int is null in c#?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1000 total points
ID: 40369668
if your variable is declare as int?, you can use YourVariableName.HasValue
0
 
LVL 14

Assisted Solution

by:frankhelk
frankhelk earned 1000 total points
ID: 40370173
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
 
LVL 2

Expert Comment

by:Pratik Makwana
ID: 40370744
I think you are looking for this.....

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question