views:

128

answers:

2

Hi,

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.

Thanks

+12  A: 

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.

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

Jon Skeet
+2  A: 

I'd suggest looking at the ECMA spec for C#, it explains quite well what a lifted operator is. ECMA-334: 14.2.7 Lifted operators

ho1