What are lifted operators?
Asked Answered
O

2

72

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 to come to terms with the VB.NET equivalent whilst reading up on nullable types probably isn't the best place to start...

Would anyone be able to provide an explanation of lifted operators and how they are used by nullable types? Does it just mean that the nullable type does not itself overload operators and will use the operators from the underlying type that it represents?

There doesn't seem to be much information on SO about lifted operators, so hopefully this can help some others out too.

Offering answered 30/7, 2010 at 9:13 Comment(1)
Ah.. Thought that (null >> 1) was invalid and was wondering why is it the case that ?? seems to operate on x in the second statement of (int? x = null;(x >> 1) ?? -1;). Thought that ?? "transforms" it to a valid form ((x == null) ? -1: (x >> 1);). Turns out it was operating on (x >> 1) instead, and that (null >> 1) is valid.Perez
G
139

Lifted operators are operators which work over nullable types by "lifting" the operators which already exist on the non-nullable form. So for example, if you do:

int? x = 10;
int? y = 10;
int? z = x + y;

That "+" operator is lifted. It doesn't actually exist on Nullable<int> but the C# compiler acts as if it does, generating code to do the right thing. (For the most case, that's a matter of checking whether either operand is null; if so, the result is null. Otherwise, unwrap both operands to their non-nullable values, use the normal operator, and then wrap the result back into a nullable value. There are a few special cases around comparisons though.)

See section 6.4.2 (lifted conversion operators) and 7.3.7 (lifted operators) of the C# spec for more information.

Gruchot answered 30/7, 2010 at 9:19 Comment(4)
This answer doesn't say anything about what "the right thing" actually is, which I would think is the most important aspect of how lifted operators work.Onlybegotten
@JonSkeet: Your link directs to the C# 5 spec; did you mean to say 5 here or link to the 4 spec?Thailand
@Cᴏʀʏ: It was the C# 4 spec when I linked to it, but that link now goes to the C# 5 spec... it might be the C# 6 spec in a few months :)Gruchot
@JonSkeet: I suppose it doesn't really matter anyway, this answer, no matter which spec, should be relevant for a long time.Thailand
P
4

Lifted operators allow predefined and user-defined operators that work for non-nullable types to be used for their nullable forms as well.

int i = 5;
int? j = 6;

int? k = j + i;    // 11
int? q = i + null; // null - Shows a warning the result of the expression is always null of type int?
int r = i + null; // Throws an error the result of expression is always null of type int?
Profusive answered 4/5, 2019 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.