11

When you try to cast a value from a type to another incompatible type, you get the following error in C#:

CS0039 Cannot convert type A to B via reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

I understand how to potentially fix this, but my question is about the types of conversions themselves. It mentions reference (superclass to subclass or vice versa), boxing and unboxing (value type to object) and null type (e.g. int to int?) conversions, but then what is a wrapping conversion? This one isn't obvious to me, and searching for this term just brings back results about error CS0039, instead of an explanation of the concept.

1
  • @Alex: That's just a user-defined conversion operator, which cannot be used with as.
    – Joey
    Feb 27, 2018 at 16:25

2 Answers 2

13

Wrapping converts a non-nullable value type to its nullable equivalent. Unwrapping is the reverse. For example:

int x = 5;
int? y = x; // Wrapping
int z = (int) y; // Unwrapping

The C# spec doesn't actually call these "wrapping conversions" and "unwrapping conversions" but it does talk about wrapping and unwrapping. From section 4.1.10 of the C# 5 spec, or the online spec (emphasis mine):

An instance for which HasValue is false is said to be null. A null instance has an undefined value. Attempting to read the Value of a null instance causes a System.InvalidOperationException to be thrown. The process of accessing the Value property of a nullable instance is referred to as unwrapping. In addition to the default constructor, every nullable type T? has a public constructor that takes a single argument of type T. Given a value x of type T, a constructor invocation of the form

new T?(x)

creates a non-null instance of T? for which the Value property is x. The process of creating a non-null instance of a nullable type for a given value is referred to as wrapping.

7
  • Free link to the spec where this is mentioned. Feb 27, 2018 at 16:25
  • 1
    To add to this, this is what Arturo mistakenly assumed to be »null type conversion«, which I think refers to converting the null literal to any compatible type instead.
    – Joey
    Feb 27, 2018 at 16:26
  • @JeroenMostert: Thanks, was just adding the quote too. Will add a link... although that's currently the draft C# 6 spec, the URL suggests it will always be "current".
    – Jon Skeet
    Feb 27, 2018 at 16:28
  • 1
    @JeroenMostert: The "null type" used to be in the spec but Mads and I removed it as it was unnecessary. Apparently I forgot to update the error message when we did. Feb 27, 2018 at 16:32
  • 2
    @EricLippert: I bet some really obscure but incredibly important piece of software run by a Fortune 500 company will break if you do it now because it's matching on the text... but hey, at least we now know what's what. :-) Feb 27, 2018 at 16:36
4

As stated in the spec:

If the nullable conversion is from S to T?, the conversion is evaluated as the underlying conversion from S to T followed by a wrapping from T to T?.

Which implicitly means that wrapping means to turn a non-nullable into it's nullable type variant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.