0
0
C Sharp (C#)programming~10 mins

Verbatim and raw string literals in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Verbatim and raw string literals
Start
Choose string type
Treats backslashes and newlines literally
Multi-line, preserves formatting, no escapes
Use string in code
Output string as-is
End
This flow shows choosing between verbatim (@) and raw ("""") string literals, then using and outputting them literally.
Execution Sample
C Sharp (C#)
string path = @"C:\Users\Name\Documents";
string raw = """
Line1
Line2\nLine3
""";
Console.WriteLine(path);
Console.WriteLine(raw);
This code defines a verbatim string for a file path and a raw string with multiple lines, then prints both.
Execution Table
StepActionEvaluationResult
1Assign verbatim string to pathpath = @"C:\Users\Name\Documents"path = C:\Users\Name\Documents
2Assign raw string to rawraw = """ Line1 Line2\nLine3 """raw = "Line1 Line2\nLine3 " (3 lines, literal backslash n)
3Print pathConsole.WriteLine(path)Output: C:\Users\Name\Documents
4Print rawConsole.WriteLine(raw)Output: Line1 Line2\nLine3
5End of programProgram ends
💡 Program ends after printing both strings literally.
Variable Tracker
VariableStartAfter 1After 2Final
pathnullC:\Users\Name\DocumentsC:\Users\Name\DocumentsC:\Users\Name\Documents
rawnullnullLine1 Line2\nLine3 (3 lines)Line1 Line2\nLine3 (3 lines)
Key Moments - 3 Insights
Why does the verbatim string keep double backslashes?
Because verbatim strings treat backslashes literally, so you write double backslashes to represent one actual backslash. See step 1 in execution_table.
Why does the raw string print \n instead of a new line?
Raw strings do not process escape sequences like \n, so it prints the characters \ and n literally. See step 4 in execution_table.
How do verbatim and raw strings handle new lines differently?
Verbatim strings can include new lines directly, but raw strings preserve all formatting exactly as typed, including new lines and spaces. See variable_tracker for raw string content.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'path' after step 1?
AC:\\Users\\Name\\Documents
BC:/Users/Name/Documents
CC:\Users\Name\Documents
DC:UsersNameDocuments
💡 Hint
Check the 'Result' column in row 1 of execution_table.
At which step does the program print the raw string?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for Console.WriteLine(raw) in execution_table.
If you want the raw string to interpret \n as a new line, what should you do?
AUse verbatim string instead
BReplace raw string with normal string with escapes
CAdd escape sequences inside raw string
DRaw strings always interpret \n as new line
💡 Hint
Raw strings do not process escapes, see key_moments about raw string behavior.
Concept Snapshot
Verbatim strings start with @ and treat backslashes and new lines literally.
Raw strings use triple quotes """ and preserve all formatting exactly.
Verbatim strings require double backslashes for one backslash.
Raw strings do not process escape sequences like \n.
Use verbatim for Windows paths, raw for multi-line text.
Print outputs strings as they are written.
Full Transcript
This example shows how C# uses verbatim strings with @ and raw strings with triple quotes """. Verbatim strings treat backslashes literally, so double backslashes represent one backslash. Raw strings preserve all formatting including new lines and backslashes without processing escape sequences. The code assigns a verbatim string to 'path' and a raw string to 'raw', then prints both. The output shows the verbatim string as a Windows path with backslashes, and the raw string as multiple lines with literal \n characters. This helps beginners understand how these string types handle special characters and formatting differently.