Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Verbatim and Raw String Literals in C#
📖 Scenario: You are creating a simple program to store and display file paths and multi-line text messages exactly as they are written, including special characters and line breaks.
🎯 Goal: Build a C# program that uses verbatim and raw string literals to store and print a file path and a multi-line message with quotes and line breaks preserved.
📋 What You'll Learn
Create a string variable called filePath using a verbatim string literal with the exact value C:\Users\Public\Documents\Report.txt.
Create a string variable called message using a raw string literal that contains the exact multi-line text: "Dear User,\nPlease review the attached report.\nThanks,\nAdmin" including the quotes and line breaks.
Print the filePath variable.
Print the message variable.
💡 Why This Matters
🌍 Real World
Handling file paths and multi-line text messages exactly as typed is common in configuration files, logs, and user messages.
💼 Career
Knowing how to use verbatim and raw string literals helps developers write clearer and less error-prone code when dealing with strings that contain special characters or multiple lines.
Progress0 / 4 steps
1
Create the file path string with a verbatim literal
Create a string variable called filePath using a verbatim string literal with the exact value C:\\Users\\Public\\Documents\\Report.txt.
C Sharp (C#)
Hint
Use the @"..." syntax to create a verbatim string literal in C#.
2
Create the multi-line message string with a raw string literal
Create a string variable called message using a raw string literal that contains the exact multi-line text including quotes and line breaks: "Dear User,\nPlease review the attached report.\nThanks,\nAdmin".
C Sharp (C#)
Hint
Use triple double quotes """...""" to create a raw string literal in C# 11+.
3
Print the file path string
Write a Console.WriteLine statement to print the filePath variable.
C Sharp (C#)
Hint
Use Console.WriteLine(filePath); to print the file path.
4
Print the multi-line message string
Write a Console.WriteLine statement to print the message variable.
C Sharp (C#)
Hint
Use Console.WriteLine(message); to print the multi-line message.
Practice
(1/5)
1. What is the main purpose of a verbatim string literal in C# (starting with @)?
easy
A. To write strings that ignore escape sequences and can span multiple lines easily.
B. To create strings that automatically convert to uppercase.
C. To define strings that are encrypted at compile time.
D. To declare strings that are immutable and cannot be changed.
Solution
Step 1: Understand verbatim string syntax
Verbatim strings start with @ and allow writing strings with backslashes and new lines without escape sequences.
Step 2: Identify the purpose
This makes writing file paths and multi-line text easier and more readable.
Final Answer:
To write strings that ignore escape sequences and can span multiple lines easily. -> Option A
Quick Check:
Verbatim strings ignore escapes = A [OK]
Hint: Verbatim strings start with @ and ignore escape sequences [OK]
Common Mistakes:
Thinking verbatim strings convert text case
Assuming verbatim strings encrypt content
Confusing immutability with verbatim syntax
2. Which of the following is the correct syntax for a raw string literal in C# 11+?
easy
A. """This is a raw string"""
B. @"This is a raw string"
C. 'This is a raw string'
D. "This is a raw string"
Solution
Step 1: Recall raw string literal syntax
Raw string literals in C# 11+ use triple double quotes to start and end the string.
Step 2: Compare options
"""This is a raw string""" uses triple double quotes correctly; @"This is a raw string" is verbatim string syntax, not raw string.
Final Answer:
"""This is a raw string""" -> Option A
Quick Check:
Raw strings use triple quotes = D [OK]
Hint: Raw strings use triple quotes """ at start and end [OK]
Common Mistakes:
Confusing verbatim strings (@) with raw strings (""")
Using single quotes for strings
Using normal double quotes for raw strings
3. What will be the output of this C# code?
string path = @"C:\Users\Admin";
string raw = """C:\Users\Admin""";
Console.WriteLine(path);
Console.WriteLine(raw);
medium
A. C:\\Users\\Admin
C:\\Users\\Admin
B. C:\Users\Admin
C:\Users\Admin
C. C:UsersAdmin
C:UsersAdmin
D. C:\UsersAdmin
C:\Users\Admin
Solution
Step 1: Understand verbatim string output
The verbatim string @"C:\Users\Admin" outputs the path with single backslashes because escapes are ignored.
Step 2: Understand raw string output
The raw string """C:\Users\Admin""" preserves the backslashes exactly as typed, so output is the same.
Final Answer:
C:\Users\Admin
C:\Users\Admin -> Option B
Quick Check:
Both print path with single backslashes = B [OK]
Hint: Both verbatim and raw strings preserve backslashes as typed [OK]
Common Mistakes:
Expecting double backslashes in output
Confusing escape sequences in verbatim strings
Thinking raw strings remove backslashes
4. Identify the error in this code snippet:
string text = @"This is a "quoted" word.";
medium
A. Raw string literals require triple quotes, not @.
B. Verbatim strings cannot contain double quotes.
C. No error, code is correct.
D. Missing escape for inner quotes inside verbatim string.
Solution
Step 1: Check verbatim string rules for quotes
In verbatim strings, double quotes inside must be doubled to escape them ("" for one ").
Step 2: Identify the error
The code uses single double quotes inside verbatim string, causing syntax error.
Final Answer:
Missing escape for inner quotes inside verbatim string. -> Option D
Quick Check:
Double quotes inside verbatim need doubling = C [OK]