Advertisement
//Creating two String objects in the intern pool
//by directly assigning a String literal value
String s1 = "Sky"; // Statement1
String s2 = "Blue" // Statement2
//Dynamically creating a String object
//by programmatically adding two String objects
s1 = s1 + s2; // Statement3
Advertisement
//C# String Immutability Example
using System;
using System.Text;
class String1
{
public static void Main()
{
//Creating two String objects in the intern pool
//by directly assigning a String literal value
String s1= "Sky";
String s2= "Blue";
//Dynamically creating a String object
//by programmatically adding two String objects
s1= s1+s2; //Statement 1
Console.WriteLine("s1 points to " + s1);
Console.WriteLine("s2 points to " + s2);
//Creating a char array
char[] arr = {'N', 'e', 'w'};
//Dynamically creating a String object from a char[] array.
String s3 = new String(arr);
//Dynamically creating a String object by calling converting a StringBuilder to String object.
String s4 = new StringBuilder("York").ToString();
//Programmatically reinitializing the value of a String object
//referenced by, s3
s3= s3+s4; //Statement 2
Console.WriteLine("s3 points to " + s3);
Console.WriteLine("s4 points to " + s4);
}
}
s1 points to SkyBlue
s2 points to Blue
s3 points to NewYork
s4 points to York
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement