Verbatim String Literal in C#

Hi Folks,

In this short post I talk about how to write verbatim string literals in C#. You write this by using using the @ keyword just before the string. What does a verbatim string literal mean? It means that the characters in it will be interpreted verbatim. This means that escape sequences such as \ \ for backslash, hexadecimal escape sequences such as \x0041 for uppercase A, and Unicode sequences such as \u0041 for uppercase A, are interpreted literally. To demonstrate this, consider that the following two strings are equivalent.

string filename1 = @"c:\documents\file.txt";
string filename2 = "c:\\documents\\file.text";

That’s all for now. Till next time, happy software development.

 

Reference

1. Verbatim text and strings – @ – C# reference. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim

Leave a Reply

Your email address will not be published. Required fields are marked *