- Introduction
- Creating and Using String Objects
- Formatting Strings
- Accessing Individual String Characters
- Analyzing Character Attributes
- Case-Insensitive String Comparison
- Working with Substrings
- Using Verbatim String Syntax
- Choosing Between Constant and Mutable Strings
- Optimizing StringBuilder Performance
- Understanding Basic Regular Expression Syntax
- Validating User Input with Regular Expressions
- Replacing Substrings Using Regular Expressions
- Building a Regular Expression Library
3.7. Using Verbatim String Syntax
You want to represent a path to a file using a string without using escape characters for path separators.
Technique
When assigning a literal string to a string object, preface the string with the @ symbol. It turns off all escape-character processing so there is no need to escape path separators:
string nonVerbatim = "C:\\Windows\\Temp"; string verbatim = @"C:\Windows\Temp";
Comments
A compiler error that happens so frequently comes from forgetting to escape path separators. Although a common programming faux pas is to include hard-coded path strings, you can overlook that rule when testing an application. Visual C# .NET added verbatim string syntax as a feature to alleviate the frustration of having to escape all the path separators within a file path string, which can be especially cumbersome for large paths.