SlideShare a Scribd company logo
1 of 1
Download to read offline
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

More Related Content

More from Salman Mushtaq

More from Salman Mushtaq (8)

Bootstrap 3 Lecture 5
Bootstrap 3 Lecture 5Bootstrap 3 Lecture 5
Bootstrap 3 Lecture 5
 
LINQ to SQL
LINQ to SQLLINQ to SQL
LINQ to SQL
 
Captcha
CaptchaCaptcha
Captcha
 
ADO DOT NET
ADO DOT NETADO DOT NET
ADO DOT NET
 
Contact Book Version 1.0
Contact Book Version 1.0Contact Book Version 1.0
Contact Book Version 1.0
 
Entity frame work by Salman Mushtaq -1-
Entity frame work by Salman Mushtaq -1-Entity frame work by Salman Mushtaq -1-
Entity frame work by Salman Mushtaq -1-
 
Database By Salman Mushtaq
Database By Salman MushtaqDatabase By Salman Mushtaq
Database By Salman Mushtaq
 
Serialization
SerializationSerialization
Serialization
 

String in C# Part1

  • 1. 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