Skip to content

Instantly share code, notes, and snippets.

@bernardoantunes
Created July 22, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bernardoantunes/135687b9995a256a53c2 to your computer and use it in GitHub Desktop.
Save bernardoantunes/135687b9995a256a53c2 to your computer and use it in GitHub Desktop.
"Immutable" String test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestingStringImutability
{
class Program
{
static void Main(string[] args)
{
var s1 = "Immutable String";
var s2 = "Immutable String";
var s3 = s2;
var sb = new StringBuilder(s1);
var s4 = sb.ToString();
var s5 = sb.ToString();
unsafe
{
fixed (char* ptr = s1)
{
*(ptr + 1) = '_';
*(ptr + 2) = '_';
}
fixed (char* ptr = s4)
{
*(ptr + 1) = '_';
*(ptr + 2) = '_';
}
}
Console.WriteLine("S1 = " + s1); // Prints I__utable String
Console.WriteLine("S2 = " + s2); // Prints I__utable String
Console.WriteLine("S3 = " + s3); // Prints I__utable String
Console.WriteLine("S4 = " + s4); // Prints Immutable String
Console.WriteLine("S5 = " + s5); // Prints Immutable String
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment