Bird
0
0

You want to store this multi-line JSON string exactly as shown, including indentation and quotes, in C# 11+. Which is the best way to do it?

hard🚀 Application Q15 of 15
C Sharp (C#) - Strings and StringBuilder
You want to store this multi-line JSON string exactly as shown, including indentation and quotes, in C# 11+. Which is the best way to do it?
{
  "name": "Alice",
  "age": 30
}
AUse a normal string with \n for new lines and escaped quotes.
BUse a verbatim string with @ and escape all quotes with backslashes.
CUse a raw string literal with triple quotes preserving all formatting.
DUse string concatenation for each line.
Step-by-Step Solution
Solution:
  1. Step 1: Understand formatting needs

    The JSON string has multiple lines, indentation, and quotes that must be preserved exactly.
  2. Step 2: Choose best string literal

    Raw string literals with triple quotes preserve all formatting and quotes without escapes, making code clean and readable.
  3. Final Answer:

    Use a raw string literal with triple quotes preserving all formatting. -> Option C
  4. Quick Check:

    Raw strings preserve multi-line and quotes = A [OK]
Quick Trick: Use raw strings for exact multi-line text with quotes [OK]
Common Mistakes:
MISTAKES
  • Escaping quotes manually in verbatim strings
  • Using normal strings with many escapes
  • Concatenating strings unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes