Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxing, a thing of the past?

Tags:

c#

.net

Is there any point in doing this?

public static void Write<T>(T value)
{
    textWriter.Write(value.ToString());
}

...as supposed to this:

public static void Write(object value)
{
    textWriter.Write(value.ToString());
}

Setting aside the obvious null dereference possibility, If I where to write a lot of value types using this method wouldn't the former be much better because it will have it's own version of the write method to call, or is it just gonna bloat the binary in terms of a lot of additional code being generated?

The performance implication of such a thing might be negligible, but I'm curious, it's a lot more compact than providing an overload for each and every value type in the BCL, like most writers in the BCL already do.

like image 966
John Leidegren Avatar asked Aug 17 '10 05:08

John Leidegren


1 Answers

From what I understand, in both cases boxing occurs.

The latter is obvious as the value is already boxed.

The former is less obvious, but as a virtual method is called on a valuetype, it will need to be boxed to perform the callvirt.

Edit: I just checked the emitted IL, and there is no explicit boxing occuring in the generic case. Something rings a bell though.

Edit 2: I might have been confusing myself with the case using interfaces. There clearly boxing occurs.

Edit 3: Boxing does occur, if ToString() is not overriden in the value type.

I get this from ECMA-335 part 3 pg 25 (only noting the last case):

If thisType is a value type and thisType does not implement method then ptr is dereferenced, boxed, and passed as the ‘this’ pointer to the callvirt of method

This last case can only occur when method was defined on System.Object, System.ValueType, or System.Enum and not overridden by thisType. In this last case, the boxing causes a copy of the original object to be made, however since all methods on System.Object, System.ValueType, and System.Enum do not modify the state of the object, this fact can not be detected.

Edit 4: Here is a similar question on SO.

like image 59
leppie Avatar answered Sep 30 '22 15:09

leppie