Question for framework guys: string.Format and valuetype boxing

E

Eric Newton

Since String.Format has to box all value types to accomodate the params, and
just for sheer efficiency, are there possibly any plans for a FormatValue
method to minimize boxing?

public static string FormatValue(string formatSpec, System.ValueType[]
valueArgs);
public static string FormatValue(string formatSpec, System.ValueType arg1)
{
return FormatValue(formatSpec,new ValueType[] { arg1 });
}
 
D

Daniel Pratt

Hi Eric,

Eric Newton said:
Since String.Format has to box all value types to accomodate the params, and
just for sheer efficiency, are there possibly any plans for a FormatValue
method to minimize boxing?

public static string FormatValue(string formatSpec, System.ValueType[]
valueArgs);
public static string FormatValue(string formatSpec, System.ValueType arg1)
{
return FormatValue(formatSpec,new ValueType[] { arg1 });
}

Perhaps counterintuitively, System.ValueType is a reference type, not a
value type. Given the method signature above, boxing would still occur.

Regards,
Dan
 
J

Jon Skeet [C# MVP]

Eric Newton said:
Since String.Format has to box all value types to accomodate the params, and
just for sheer efficiency, are there possibly any plans for a FormatValue
method to minimize boxing?

Using ValueType itself wouldn't help - they'd still be boxed, because
ValueType itself is a reference type. (I know, it's weird...)

If you think about it, the stack needs to know how big to be in bytes,
and it's not going to know that for any random value type - it would
need to know the exact value type in question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top