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.
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):
Edit 4: Here is a similar question on SO.