Home > Articles

  • Print
  • + Share This
This chapter is from the book

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.

  • + Share This
  • 🔖 Save To Your Account