Why can the as operator be used with Nullable<T>?
Asked Answered
T

6

20

According to the documentation of the as operator, as "is used to perform certain types of conversions between compatible reference types". Since Nullable is actually a value type, I would expect as not to work with it. However, this code compiles and runs:

object o = 7;
int i = o as int? ?? -1;
Console.WriteLine(i); // output: 7

Is this correct behavior? Is the documentation for as wrong? Am I missing something?

Thready answered 18/8, 2011 at 15:12 Comment(3)
Nullable<T> is a value type, I've corrected your text.Oscaroscillate
@Matthew: Thanks. That was a rather confusing typo.Thready
Great discussion on AS/IS and Nullables - #1583550Swashbuckling
Y
21

Is this correct behavior?

Yes.

Is the documentation for as wrong?

Yes. I have informed the documentation manager. Thanks for bringing this to my attention, and apologies for the error. Obviously no one remembered to update this page when nullable types were added to the language in C# 2.0.

Am I missing something?

You might consider reading the actual C# specification rather than the MSDN documentation; it is more definitive.

Yashmak answered 18/8, 2011 at 17:33 Comment(0)
R
2

I read:

Note that the as operator only performs reference conversions and boxing conversions. The as operator cannot perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.

And boxing conversions.....

Rhoea answered 18/8, 2011 at 15:17 Comment(4)
But it's not just simple boxing, since it was just a normal int that was boxed, and a int? that's the target of the as.Thready
Why you think int? has to be managed in different way from this problem perspective?Rhoea
I don't know why it's different. In a sense, that's related to what I'm asking.Thready
Guess: considering msdn.microsoft.com/en-us/library/ms228597.aspx which confirns that Nullable type is boxed ( just like usual one) in case if it,s not Null, in this case casting behaves just like if it works with usual integers.Rhoea
C
1

Just a guess, but I'd say it boxes o as an integer and then converts it to a nullable.

Complainant answered 18/8, 2011 at 15:17 Comment(1)
Almost. It unboxes the object as an integer if it is an integer, and then converts it to a nullable. If the unboxing fails then the result is of course a null nullable int.Yashmak
P
1

From the documentation about the as keyword:

It is equivalent to the following expression except that expression is evaluated only one time.

expression is type ? (type)expression : (type)null

The reference for is use also states it works with reference types, however, you can also do stuff like this:

int? temp = null;
if (temp is int?)
{
    // Do something
}

I'm guessing it is just an inaccuracy in the reference documentation in that the type must be nullable (ie a nullable type or a reference type) instead of just a reference type.

Pestilent answered 18/8, 2011 at 15:26 Comment(0)
C
1

Apparently the MSDN documentation on the as operator needs to be updated.

object o = 7;
int i = o as **int** ?? -1;
Console.WriteLine(i);

If you try the following code where we use the as operator with the value type int, you get the appropriate compiler error message that

The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type)

There is an update though on the link in Community Content section that quotes:

The as operator must be used with a reference type or nullable type.
Copious answered 18/8, 2011 at 16:37 Comment(0)
D
0

You're applying the 'as' to Object, which is a reference type. It could be null, in which case the CLR has special support for unboxing the reference 'null' to a nullable value type. This special unboxing is not supported for any other value type, so, even though Nullable is a value type, it does have certain special privledges.

Diluent answered 18/8, 2011 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.