Creating Strings
C# treats strings as though they were built-in types (much as it does with arrays). C# strings are flexible, powerful, and easy to use.
In .NET, each string object is an immutable sequence of Unicode characters. In other words, methods that appear to change the string actually return a modified copy; the original string remains intact.
The declaration of the System.String class is (in part):
public sealed class String : IComparable, ICloneable, IConvertible, IEnumerable
This declaration reveals that the class is sealed, meaning that it is not possible to derive from the String class. The class also implements four system interfaces—IComparable, ICloneable, IConvertible, and IEnumerable—which dictate functionality that System.String shares with other classes in the .NET Framework: the ability to be sorted, copied, converted to other types, and enumerated in foreach loops, respectively.
String Literals
The most common way to create a string is to assign a quoted string of characters, known as a string literal, to a user-defined variable of type string. The following code declares a string called newString that contains the phrase “This book teaches C#”:
string newString = "This book teaches C#";
To be precise, newString is a string object that is initialized with the string literal "This book teaches C#". If you pass newString to the WriteLine method of the Console object, the string “This book teaches C#” will be displayed.
Escape Characters
Quoted strings can include escape ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access