String in C# Part1
Click here to load reader
-
Upload
salman-mushtaq -
Category
Documents
-
view
96 -
download
1
Transcript of String in C# Part1
STRINGS
Strings in C# are reference type.
Reference type is managed on heap.
They are automatically destroyed if out of scope.
No default value is assign to string so you must assign a value before accessing it.
String s1 = “This is String”;
Remember strings are immutable so it means original string never changed.
If you want to change the string you must concatenating it with ‘+’ sign.
String s1 = “”;
String s2 = s1 + “Some text”;
s1 = s2+ ”Some text”;
String is changed but behind the scene heap makes another string every time when we change
it. And stay in memory unless garbage collector called.
For this reason, .NET framework provides “StringBuilder” class and we efficiently perform
concatenating by using its method Append().
It consumes more memory but there is only one reference in memory.
If you now about the escape characters you know that w use it to print our desired string but if
we want our desired string without using escape characters just put “@” sign at the start of
string i.e.
string s1 = @“Hello I am “Salman Mushtaq””;
It generate the below output:
Hello I am “Salman Mushtaq”
Reference: Introducing C# Strings