Free mag vol1 | Page 164

CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I Table 3-6. String Literal Escape Characters Character Meaning in Life \' Inserts a single quote into a string literal. \" Inserts a double quote into a string literal. \\ Inserts a backslash into a string literal. This can be quite helpful when defining file or network paths. \a Triggers a system alert (beep). For console programs, this can be an audio clue to the user. \n Inserts a new line (on Windows platforms). \r Inserts a carriage return. \t Inserts a horizontal tab into the string literal. For example, to print a string that contains a tab between each word, you can make use of the \t escape character. Or assume you wish to create a string literal that contains quotation marks, another that defines a directory path, and a final string literal that inserts three blank lines after printing the character data. To do so without compiler errors, you would need to make use of the \", \\, and \n escape characters. Also, to annoy any person within a 10-foot radius from you, notice that I have embedded an alarm within each string literal (to trigger a beep). Consider the following: static void EscapeChars() { Console.WriteLine("=> Escape characters:\a"); string strWithTabs = "Model\tColor\tSpeed\tPet Name\a "; Console.WriteLine(strWithTabs); Console.WriteLine("Everyone loves \"Hello World\"\a "); Console.WriteLine("C:\\MyApp\\bin\\Debug\a "); } // Adds a total of 4 blank lines (then beep again!). Console.WriteLine("All finished.\n\n\n\a "); Console.WriteLine(); Defining Verbatim Strings When you prefix a string literal with the @ symbol, you have created what is termed a verbatim string. Using verbatim strings, you disable the processing of a literal’s escape characters and pr int out a string as is. This can be most useful when working with strings representing directory and network paths. Therefore, rather than making use of \\ escape characters, you can simply write the following: 98