Skip to content

Instantly share code, notes, and snippets.

@jskeet
Created April 9, 2017 17:08
Show Gist options
  • Save jskeet/96fa90756e57f6553bcb0f7fc8c863db to your computer and use it in GitHub Desktop.
Save jskeet/96fa90756e57f6553bcb0f7fc8c863db to your computer and use it in GitHub Desktop.
using System;
using System.Text;
class Test
{
// Notes:
// The target of the builder.Length assignment is evaluated before
// any of the assignments actually happen - in particular, before the
// builder assignment from the first element of the tuple.
//
// In the Reflector-decompiled code, the new value of builder is
// used, so the original StringBuilder isn't truncated.
static void Main()
{
StringBuilder builder = new StringBuilder("123456789");
StringBuilder original = builder;
(builder, builder.Length) = (new StringBuilder(), 5);
Console.WriteLine(original); // Prints 12345
}
private static void MainAsDecompiledInReflector9_0_2_546()
{
StringBuilder builder = new StringBuilder("123456789");
StringBuilder builder2 = builder;
ValueTuple<StringBuilder, int> tuple1 = new ValueTuple<StringBuilder, int>(new StringBuilder(), 5);
StringBuilder builder3 = tuple1.Item1;
int num = tuple1.Item2;
builder = builder3;
builder.Length = num;
Console.WriteLine(builder2); // Prints 123456789
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment