Created
July 22, 2015 11:39
-
-
Save bernardoantunes/135687b9995a256a53c2 to your computer and use it in GitHub Desktop.
"Immutable" String test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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