Bird
0
0

Which of the following is the correct way to declare a string variable in C#?

easy📝 Syntax Q12 of 15
C Sharp (C#) - Strings and StringBuilder
Which of the following is the correct way to declare a string variable in C#?
Astring name = Alice;
BString name = 'Alice';
Cvar name = Alice;
Dstring name = "Alice";
Step-by-Step Solution
Solution:
  1. Step 1: Check string declaration syntax

    In C#, strings are declared with the keyword string and text is enclosed in double quotes.
  2. Step 2: Validate each option

    string name = "Alice"; uses correct syntax: string name = "Alice";. String name = 'Alice'; uses single quotes which are for characters, not strings. var name = Alice; misses quotes around text. string name = Alice; misses quotes around text.
  3. Final Answer:

    string name = "Alice"; -> Option D
  4. Quick Check:

    Use double quotes for strings [OK]
Quick Trick: Use double quotes and 'string' keyword for text variables [OK]
Common Mistakes:
MISTAKES
  • Using single quotes for strings
  • Forgetting quotes around text
  • Using var without quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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