Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this a valid comparison [duplicate]

Tags:

c#

Here is some example code:

static DateTime time;
if (time == null)
{
/* do something */
}

Since DateTime cannot be null, why does this code compile?

Edit: The issue is not just that this code will always return false,but why something like DateTime which is never null is allowed in such a comparison.

like image 823
Rinus Avatar asked Apr 23 '15 11:04

Rinus


People also ask

What are duplicates in research?

To be considered duplicates, two or more citations had to share the same author, title, publication date, volume, issue, and start page information. The full-text versions of the citations were consulted when we were in doubt.

Why is it important to remove duplicate data?

Datasets that contain duplicates may contaminate the training data with the test data or vice versa. Entries with missing values will lead models to misunderstand features, and outliers will undermine the training process – leading your model to “learn” patterns that do not exist in reality.

How do you find duplicates in a systematic review?

Go to References > Find Duplicates (Or go to Library > Find Duplicates in Endnote 20). This helps you find duplicates across all databases in your library. You will then be asked to review the duplicate record one by one. To review duplicates in one go, click "Cancel".


1 Answers

Although time is of a non-nullable value type, it can be converted to nullable and compared to null. The comparison would yield false, which is a valid outcome.

This does not mean, however, that it is a good code. Tools, such as re:sharper, would flag this line with a warning saying "Expression is always false".

like image 108
Sergey Kalinichenko Avatar answered Sep 28 '22 08:09

Sergey Kalinichenko