Solved

How to check if int is null in c#?

Posted on 2014-10-08
4
1,543 Views
Last Modified: 2014-10-09
How to check if int is null in c#?
0
Comment
Question by:developer2012
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Turn on intranet settings 1 55
Web Form VB.Net  import CSV 4 36
Windows Media Player 2 26
C# Gridview 1 28
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now