Questions tagged [lifted-operators]

Lifted operators are operators which work over nullable types by "lifting" the operators which already exist on the non-nullable form.

Lifted operators are operators which work over nullable types by "lifting" the operators which already exist on the non-nullable form.

The answer What are lifted operators is a good overview with additional references.

7 questions
70
votes
2 answers

What are lifted operators?

I was looking at this article and am struggling to follow the VB.NET example that explains lifted operators. There doesn't seem to be an equivalent C# example or tutorial. I don't have much experience with operator overloading in general, so trying…
fletcher
  • 13,380
  • 9
  • 52
  • 69
19
votes
3 answers

Why are there no lifted short-circuiting operators on `bool?`?

Why doesn't bool? support lifted && and ||? They could have lifted the true and false operators which would have indirectly added lifted && and ||. The operators | and & are already lifted and implement the correct Three-valued logic. But of course…
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
11
votes
3 answers

Wrong compiler warning when comparing struct to null

Consider the following code: DateTime t = DateTime.Today; bool isGreater = t > null; With Visual Studio 2010 (C# 4, .NET 4.0), I get the following warning: warning CS0458: The result of the expression is always 'null' of type 'bool?' This is…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
5
votes
1 answer

Curious overload resolution when using a naked null literal with user-defined operators

(The "user-defined" in the title refers to the fact that addition and subtraction of TimeSpan and DateTime are not a part of the C# standard. They are defined in the BCL.) Playing around with lifted operators on nullable TimeSpan and DateTime…
4
votes
4 answers

System.Nullable What is the value of null * int value?

Consider the following statements: int? v1 = null; int? v2 = 5 * v1; What is the value of v2? (null or empty string?) How can I prevent the compiler to mark it as invalid operation? Do I need to follow custom exception handling?
user160677
  • 4,233
  • 12
  • 42
  • 55
2
votes
2 answers

Comparing two null nullables

The C# 5.0 spec reads in chapter 7.1.3 https://msdn.microsoft.com/en-us/library/ms228593.aspx The lifted operator produces the value false if one or both operands are null. However testing and also this MSDN link…
Dee J. Doena
  • 1,631
  • 3
  • 16
  • 26
1
vote
0 answers

Do lifted operators on nullable value types employ short-circuiting?

I wonder whether code like this should execute GetAnotherValue()? int? x1 = null; var check = x1 <= GetAnotherValue(); I assume it shouldn't since that sounds more reasonable to me. But I'm not sure if this is guaranteed. As far as I can find, the…
relatively_random
  • 4,505
  • 1
  • 26
  • 48