While working on something else lately I run into a bit strange piece of code on KeyValuePair<TKey, TValue>.ToString()
implementation.
public override string ToString()
{
StringBuilder stringBuilder = StringBuilderCache.Acquire(16);
stringBuilder.Append('[');
if (this.Key != null)
{
StringBuilder arg_33_0 = stringBuilder;
TKey tKey = this.Key;
arg_33_0.Append(tKey.ToString());
}
stringBuilder.Append(", ");
if (this.Value != null)
{
StringBuilder arg_67_0 = stringBuilder;
TValue tValue = this.Value;
arg_67_0.Append(tValue.ToString());
}
stringBuilder.Append(']');
return StringBuilderCache.GetStringAndRelease(stringBuilder);
}
Skipping StringBuilderCache
class usage (which is really nice example of performance improvements in .NET itself) I have a question:
Why is
if (this.Key != null)
{
StringBuilder arg_33_0 = stringBuilder;
TKey tKey = this.Key;
arg_33_0.Append(tKey.ToString());
}
better then
if(this.Key != null)
{
stringBuilder.Append(this.Key.ToString());
}
?
What are the advantages of assigning new local variables instead of using the instances directly?