Reputation: 351
Given the following assumptions in C#:
int i = 123; object box = i;
int? n = 0;
My Question: for only reference type variables are nullable, can I say that in the second example I'm doing implicit boxing? In other terms, when I use the "?" operator to make nullable an integer, is there boxing operation implied as well (even if it's not explicit)?
Upvotes: 0
Views: 209
Reputation: 36371
int?
is suggar for Nullable<T>
See documentation. If we look at the signature of this we see:
public struct Nullable<T> where T : struct
{
...
public override bool Equals(object other)
{
if (!this.hasValue)
return other == null;
return other != null && this.value.Equals(other);
}
Since it is a struct the value will not be boxed.
If you need to compare values, n.Equals(1)
would cause boxing of the argument. I cannot find any documentation about the equality operator ==
, but I think it would be fairly safe to assume that it should not cause boxing.
Upvotes: 1