Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# do you need to check if something has a value and if something is greater than 0?

Working on a project and the coder does this a lot in his checks. First he checks if the nullable int has a value, and then he checks if its greater than 0. Why? Why make two checks if one check - if it is greater than 0 - should be sufficient? Because nulls are not greater than 0 so ...Is that redundant?

Wasn't sure if this was something I'd ask here but I wouldn't know how to word it in a google search so maybe I don't know something that this programmer does.

like image 714
egucciar Avatar asked Apr 18 '12 15:04

egucciar


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


3 Answers

The code is probably redundant.

If i is int? then:

if (i.HasValue && i.Value > 0)

is equivalent to:

if (i > 0)

From MSDN:

When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for != (not equal). It is important not to assume that because a particular comparison returns false, the opposite case returns true. In the following example, 10 is not greater than, less than, nor equal to null. Only num1 != num2 evaluates to true.

like image 63
Mark Byers Avatar answered Nov 16 '22 22:11

Mark Byers


It might be that the value for the variable has different meanings in that context.

int? someNumber = null; //might mean "there is no value"
int? someOtherNumber = 0; //might mean "the user has selected: 0"
like image 22
Ropstah Avatar answered Nov 16 '22 22:11

Ropstah


The following:

class Program {
    static void Main(string[] args) {
        int? i = null;
        if (i > 0) { Console.WriteLine(">0");
        } else {     Console.WriteLine("not >0");
        }
        if (i < 0) { Console.WriteLine("<0");
        } else {     Console.WriteLine("not <0");
        }
        if (i == 0) {Console.WriteLine("==0");
        } else {     Console.WriteLine("not ==0");
        }
        Console.ReadKey();
    }
}

will output

not >0
not <0
not ==0

without throwing an exception. So the null/HasValue check in this case is redundant. There is one small difference. The following:

(i.HasValue && (i.Value == 0))

is about twice as fast as

(i == 0)

when i is null although both are so fast it's not an important difference. When i has a value, the two comparisons take about the same amount of time.

like image 4
hatchet - done with SOverflow Avatar answered Nov 16 '22 23:11

hatchet - done with SOverflow