views:

267

answers:

5

stupid question but this statement is worthless

int a;

if (a != null)

since an integer var is automatically set to null by the compiler when defined

to check integers always check if a >= 0 correct?

A: 

int is a class, not equal to null

sorry, late night. see update
+2  A: 

The compiler sets the value of a primitive variable to its "default" value if you don't assign it. The default value of int is 0. So yeah, the comparison you mention doesn't really do anything.

If you need a nullable int in your code you should use the "nullable" type "int?".

If your int is nullable, then the comparison you mention might be useful.

Andy White
thanks. What if not using a nullable type, then my assumption holds true?
A basic "int" can never be null, so you wouldn't need to check for null. If your code needs it to be null, you should use int? rather than a "magic int value" that means not assigned.
Andy White
You might want to check int.minvalue in the case of a non-nullable int. Again, depends on your scenario.
Chuck
A: 

Yes unless the integer happens to be a nullable type.

int? myInt = null;

if(myInt == null)
{
    Console.WriteLine("myInt was null.");
}
sipwiz
A: 

Assuming by your tag & syntax this is C#, have you tried int.TryParse(...) in your particular scenario? Not really sure what you're trying to do from the code provided - as Andy mentioned if your int is nullable the null check might be useful.

Chuck
code is checking for a SelectedIndex to see if it's got a value (user selected a value from a radiobuttonlist or something)
A SelectedIndex of -1 indicates nothing was selected. Anything else indicates the position of the ListItem in the Items collection (zero based).
Chuck
A: 

An int is a value type and will never be null, unless you declare it as a nullable integer:

int? i = null;
if (i == null)
    Console.WriteLine("it's null");
Jim H.
ah, so it's initialized to 0 by the compiler.
good link I found: http://zone.ni.com/devzone/cda/ph/p/id/4