Runtime: Proposal: add overloads for methods like string.Format(), using ValueTuple as parameter to avoid boxing

Created on 26 Jul 2017  路  5Comments  路  Source: dotnet/runtime

If we could use ValueTuple as string.Format()'s parameter, many boxing could be avoided.
```C#
public class String {
public static string Format(string format, ValueTuple args) { ... }
public static string Format(string format, ValueTuple args) { ... }
public static string Format(string format, ValueTuple args) { ... }
}

......

int n=10;
string s="aaa";
bool c=true;
var t=string.Format("{0},{1},{2}", (n, s, c)); //no boxing!
Console.WriteLine("{0},{1},{2}", (c, s, n)); //Other methods like string.Format() should have this kind of overload too
```

Most helpful comment

I'm talking about the version without tuples as the one with tuples is a dead end

At which point this is largely duplicative of https://github.com/dotnet/corefx/issues/1514.

All 5 comments

The only way you can pass N arbitrary values to a method without boxing is to make the method generic with that arity. Once you do that, you may as well use plain arguments instead of taking a tuple.

If it was even possible to pass ValueTuple<,,> as ValueTuple, there would necessarily be boxing involved somewhere in getting the values out. No free lunch.

And to continue from the csharplang issue: such an approach (and I'm talking about the version without tuples as the one with tuples is a dead end) would generate a lot of code. Call to Format("", 1, 2)? One piece of code. Call to Format("", 2, 4.0)? Another piece of code. And so on for every argument combination that involves value types. And it would quite a bit of code if you really want to avoid adding new allocations in the formatting code.

I'm talking about the version without tuples as the one with tuples is a dead end

At which point this is largely duplicative of https://github.com/dotnet/corefx/issues/1514.

@stephentoub
Yes, very similar. So I closed this.

Was this page helpful?
0 / 5 - 0 ratings