This question already has answers here:
Comparing nullable DateTime?
(5 answers)
Closed 9 years ago.
i am trying to determine if there is a difference between 2 nullable dates . . is there a more elegant ways instead of this:
if (newDate.HasValue && (!oldDate.HasValue || (oldDate.HasValue && oldDate.Value.Date != mewDate.Value.Date)))
The C# compiler automatically lifts logical operators like == and != over nullalbe types, so you can usually compare them directly instead of checking HasValue. In this case, you can compare with != once you have checked that newDate is not null.
if(newDate.HasValue && newDate != oldDate)
It is described in the specification:
7.3.7 Lifted operators
Lifted operators permit predefined and user-defined operators that
operate on non-nullable value types to also be used with nullable
forms of those types.
For the equality operators
== != a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool.The
lifted form is constructed by adding a single ? modifier to each
operand type. The lifted operator considers two null values equal, and
a null value unequal to any non-null value. If both operands are
non-null, the lifted operator unwraps the operands and applies the
underlying operator to produce the bool result.
Related
This question already has answers here:
Why can't the operator '==' be applied to a struct and default(struct)?
(6 answers)
Closed 21 days ago.
in C# I know that if we try to compare two structs with "==" operator
S1 structObj1 = new S1(); //while S1 is a struct
S1 structObj2 = new S1();
if(structObj1 == structObj2)
{}
it will fire a compile error because structs are stored in the stack and the "==" operator compares references...
but why doesn't this apply when we compare two integers or chars which are struct objects?
aren't they stored in stack too?
Because == is an identity semantic operation.
If you override Equals() and make a struct comparison method inside of it, you'll be able to compare structs via. value semantics. Although I can't confirm the actual comparison result from doing that change.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Here are operators I need help with:
% (for example A%B)
!= (a%b != 0)
&(&&)
I'm very new to C# , so please try to explain me as simple as possible.
% Operator (C# Reference)
The % operator computes the remainder after dividing its first operand
by its second. All numeric types have predefined remainder operators.
User-defined types can overload the % operator (see operator). When a
binary operator is overloaded, the corresponding assignment operator,
if any, is also implicitly overloaded.
!= Operator (C# Reference)
The inequality operator (!=) returns false if its operands are equal,
true otherwise. Inequality operators are predefined for all types,
including string and object. User-defined types can overload the !=
operator.
For predefined value types, the inequality operator (!=) returns true
if the values of its operands are different, false otherwise. For
reference types other than string, != returns true if its two operands
refer to different objects. For the string type, != compares the
values of the strings.
User-defined value types can overload the != operator. So can
user-defined reference types, although by default != behaves as
described above for both predefined and user-defined reference types.
If != is overloaded, == must also be overloaded. Operations on
integral types are generally allowed on enumeration.
& Operator (C# Reference)
The & operator can function as either a unary or a binary operator.
The unary & operator returns the address of its operand (requires
unsafe context).
Binary & operators are predefined for the integral types and bool. For
integral types, & computes the logical bitwise AND of its operands.
For bool operands, & computes the logical AND of its operands; that
is, the result is true if and only if both its operands are true.
The & operator evaluates both operators regardless of the first one's
value.
&& Operator (C# Reference)
The conditional-AND operator (&&) performs a logical-AND of its bool
operands, but only evaluates its second operand if necessary.
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.
This question already has answers here:
? (nullable) operator in C# [duplicate]
(5 answers)
Closed 7 years ago.
I have some code: partial void FuncName(TimeSpan? value) What does having the nullable operator there allow me to do? If it wasn't there could I not pass a null TimeSpan at all? Do I need it for every argument I pass that could be null?
Thanks
The ? is for nullable, it will allow you to pass null in the TimeSpan.
Nullable Types
Nullable types are instances of the System.Nullable struct. A
nullable type can represent the correct range of values for its
underlying value type, plus an additional null value. For example, a
Nullable, pronounced "Nullable of Int32," can be assigned any
value from -2147483648 to 2147483647, or it can be assigned the null
value.
The sign ? with DataType indicates that Datatype is nullable.
You can read more about Nullable types here
This question already has answers here:
Direct casting vs 'as' operator?
(16 answers)
Casting vs using the 'as' keyword in the CLR
(18 answers)
What is the difference between casting and using "as" in C#?
(8 answers)
Closed 10 years ago.
Assume, that there is an interface A and a variable 'x' of a class that implements that interface. Now I can perform these:
var a = (A) x;
Or:
var a = x as A;
I know that in the case of failure, the first statement will throw an InvalidCastException and the second will return null. But is there any other difference? Particurarly in performance?
By doing (A)x you are doing a cast which will definatly try and cast, if it can't cast there will be an exception.
If you use as it will either cast or be null.
However, you have all the example code you need to try this yourself so you could of potentially tried this before asking us what the code you have stated is going to do.
The as operator is like a cast operation. However, if the conversion isn't possible, as returns null instead of raising an exception.
You can use the as operator to perform certain types of conversions between compatible reference types or nullable types.
Consider the following example:
expression as type
The code is equivalent to the following expression except that the expression variable is evaluated only one time.
expression is type ? (type)expression : (type)null
Note that the as operator performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.
Refer:
as (C# Reference)
The first one attempts to convert immediately, the second one actually checks whether x is of type A.