Bird
Raised Fist0

You want to create a string that contains the exact text:

hard🚀 Application Q15 of Q15
C Sharp (C#) - Strings and StringBuilder
You want to create a string that contains the exact text:
C:\Users\Admin\Documents

Which of the following C# declarations will produce this exact string value?
Astring path = "C:\Users\Admin\Documents";
Bstring path = @"C:\Users\Admin\Documents";
Cstring path = "C:\\Users\\Admin\\Documents";
Dstring path = 'C:\Users\Admin\Documents';
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to represent backslashes in strings

    In normal strings, backslash is an escape character, so to represent one backslash, you must write \\.
  2. Step 2: Check each option for resulting string

    string path = @"C:\Users\Admin\Documents"; uses verbatim string which preserves backslashes literally, producing the exact text with single backslashes. string path = 'C:\Users\Admin\Documents'; uses single quotes which is invalid for strings. string path = "C:\Users\Admin\Documents"; has single backslashes which are escape sequences and will produce the correct path string with single backslashes. string path = "C:\\Users\\Admin\\Documents"; doubles each backslash, resulting in double backslashes in the string, which is not the exact text.
  3. Final Answer:

    string path = @"C:\Users\Admin\Documents"; -> Option B
  4. Quick Check:

    Verbatim string preserves backslashes literally = D [OK]
Quick Trick: Use verbatim string (@) to preserve backslashes literally [OK]
Common Mistakes:
MISTAKES
  • Using single quotes for strings
  • Doubling backslashes unnecessarily in verbatim strings
  • Confusing verbatim and normal string rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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