In C#, is boxing and unboxing involved when casting long to ulong and vice versa?

219 Views Asked by At

I know that boxing then unboxing is computationally expensive, and it happens when casting a value type to object or other reference type then back to value type. However, does this happen when I'm casting long to ulong or vice versa.

I have following code:

ulong number = (ulong)IPAddress.NetworkToHostOrder((long)BitConverter.ToUInt64(bytes));

My understanding is, since both long and ulong are value type, casting between them will not have any boxing or unboxing involved.

I just want to be sure, does the code above have boxing and unboxing involved?

2

There are 2 best solutions below

2
Robert Harvey On BEST ANSWER

ulong and long are both value types. Boxing only occurs if you convert a value type to a reference type.

The source code for NetworkToHostOrder is here. As you can see, no boxing occurs there either; it's just bit operations.

Some weirdness may occur if you use a checked conversion from ulong to long that is outside of the range of the long, because an exception object will be created. But you probably have larger problems at that point.

0
JustBeingHelpful On

You are correct in your assumptions.

These are both value types and both are 8 bytes each. Since you are changing from value type to another value type, it's neither. When you convert from one to the other, you are just changing the bit that controls the sign of the number--meaning if it's a positive or negative number.

So this is not boxing nor unboxing. This is called explicit type casting, when you convert from long to ulong, or vice versa.

What is boxing? Boxing is the process of converting a value type to the object type or any interface type implemented by this value type. Boxing is implicit.

What is Unboxing? Unboxing is the reverse of boxing. It is the process of converting a reference type to value type. Unboxing extract the value from the reference type and assign it to a value type.

C# Explicit Type Conversion (Cast) Explicit conversion is required when the data cannot convert from one simple-type to another automatically by the compiler or when conversion may change the value by producing an incorrect result due to the possible loss of data (narrowing conversions). To prevent a compilation error in cases where information may be lost due to an implicit conversion between types, the compiler requires you to use a cast operator to perform the type conversion. When using explicit conversion, you deliberately force the compiler to convert the value even though there is a loss of information. Keep in mind that all casts are potentially unsafe.

C# Implicit Type Conversion (Hidden) If one type is converted into another type automatically by the CLR, it’s called implicit type conversion. No special casting syntax is required, and no data is lost during implicit conversion. Despite the fact that loss of precision may occur during the conversion process, which is allowed and never results in a run-time exception by the compiler, a widening primitive conversion always succeeds, where conversion is done from a smaller to a bigger data type without any risk of data being lost at all—for instance, casting an int to a double.

https://codebuns.com/csharp-basics/type-conversion/