String
String is a data type that can hold texts. String is reference type variable. But when you pass a string to a variable or a method, it does not reference variable. Because string is immutable reference type. That means you cannot change string after it is created. Every change to a string will create a new string.
Let’s look at the following example,
using System; namespace ZeroToCSharp { class Program { static void Main(string[] args) { string text1 = "C#"; string text2 = text1; text2 = "React"; //To print output Console.WriteLine(string.Concat("Text1: ", text1)); Console.WriteLine(string.Concat("Text2: ", text2)); Console.ReadKey(); } } }
Output:
Text1: C# Text2: React
Although string is reference type, changing copied variable has not affected main variable. Because string is immutable, so any change to a string will create a new string as i said before.
Subscribe
0 Comments