- Hands-On Object:Oriented Programming with C#
- Raihan Taher
- 159字
- 2021-07-02 12:44:35
String
A string is a sequence of characters. In C#, a string is represented by double quotation marks. There are different ways a string can be created in C#. Let's look at the different ways of creating a string in C#:
string s = "hello world";
string s1 = "hello \n\r world"; //prints the string with escape sequence
string s2 = @"hello \n\r world"; //prints the string without escape sequence
string s3 = $"S1 : {s1}, S2: {s2}"; // Replaces the {s1} and {s2} with values
The @ character can be placed as a prefix before a string to take the string as it is, without worrying about any escape characters. It is called a verbatim string. The $ character is used as a prefix for string interpolation. In case your string literal is preceded with the $ sign, the variables are automatically replaced with values if they're placed within { } brackets.