Solved

How to check if int is null in c#?

Posted on 2014-10-08
4
1,924 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 250 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 250 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Part One of the two-part Q&A series with MalwareTech.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

615 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