Why is this a valid comparison [duplicate] - c#

This question already has answers here:
C# okay with comparing value types to null
(11 answers)
Closed 7 years ago.
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.

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".

Related

C#: How to find difference of days between 2 nullable datetimes? [duplicate]

This question already has answers here:
C# - Get date difference of Nullable DateTime in int
(2 answers)
How to subtract 2 nullable DateTimes in C#? [closed]
(3 answers)
Closed 3 years ago.
A similar question was posted here in regards to calculating the difference in days of 2 datetimes, however both of my datetimes are nullable, which prevents me from using "TotalDays" as is suggested in that question.
DateTime? startDate;
DateTime? endDate;
return(d1-d2).TotalDays; //This won't work
Any idea how to fix this?
Thanks
You can do this:
if (startDate.HasValue && endDate.HasValue)
{
return (startDate.Value - endDate.Value).TotalDays;
}
else
{
// handle one or more dates being null
}
If the value it's null that will throw an exeption,
to prevent this just add a "?" like this:
return(d1-d2)?.TotalDays;
So if the object is null it won't go any further

Why is Object.ReferenceEquals(null of a nullable primitive) true? [duplicate]

This question already has answers here:
How is the boxing/unboxing behavior of Nullable<T> possible?
(3 answers)
Why does Nullable<T> HasValue property not throw NullReferenceException on Nulls?
(5 answers)
Closed 4 years ago.
So we can have nullable primitive types
bool? x = null;
Which is great. But my understanding has always been that it's not actually a null reference since
x.HasValue; //false
is possible.
I had always assumed that nullables were classes or structures with operator and Equals overloading implemented but today I checked
Object.ReferenceEquals(null, x); //true
And I have no clue how that can be.
How can it at the same time be a reference that points to null and have non-extension properties on it that are invokable?!
If this is a compiler trick, does anyone know documentation on what exactly it does?

Why does it make sense in C# to compare nullable and non-nullable int? [duplicate]

This question already has answers here:
Why "long value" equals to null is allowed?
(3 answers)
How does comparison operator works with null int?
(5 answers)
Closed 7 years ago.
I've always used the coalescing operator (a.k.a. "really surprised operator, i.e. "??") to get rid of phony nullables (usually fetched from DB as allowing nulls but known to me never to be at that value). It looks like so.
int serious = GetSomeReallyNonNullValue();
int? phony = GetNullableButActuallyNonNullValue();
int result = serious + (phony ?? 0);
However, I just noticed that the below actually compiled. Can't see how it makes sense. And I can't see intuitively if null value will evaluate the expression to true or false...
int? test = null;
if (test < 1337) ;
A lot has been written about "lifting" operations in C# (eg. here), where operators with Nullable<T> arguments are treated as operators on T when all operands are non-null. And null is only equivalent to itself.
usually fetched from DB as allowing nulls but known to me never to be at that value
In which case why is the column not set to not null?
The lifting is there because so many databases have nullable columns when they should not be.

What does a questionmark (?) mean in a function declaration in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does “DateTime?” mean in C#?
What does the ? mean after a type?
I had a function declaration including a questionmark after the datatype like:
private TimeSpan? sometime()
{
}
What does this mean?
TimeSpan? is shorthand for System.Nullable<TimeSpan>.
A TimeSpan is a value type, which cannot take a null value. By wrapping it in a System.Nullable<> it can be null. Without that ? it would be illegal to return null from the function.
Nullable Structure
Represents an object whose underlying type is a value type that can
also be assigned null like a reference type.
Instead of writing Nullable<TimeSpan>, you can write TimeSpan?.
Nullable, that a value type can be null.
It means that the value type is a nullable type
Basically its a nullable TimeStamp.

How to determine programmatically in c# that a type accepts null value or not? [duplicate]

This question already has answers here:
Closed 14 years ago.
I know merely checking for whether the type is not a value type is not sufficient. How can i account for those Nullable?
DUPLICATE
How to check if an object is nullable?
You can use Nullable.GetUnderlyingType that will return null if the type is not nullable.
have you tried the keyword default(YourType) ?
Whether T will be a reference type or a value type.
If T is a value type, whether it will be a numeric value or a struct.
This also works:
bool nullable = yourType.IsGenericType && yourType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))

Categories