Bird
0
0

Which of the following correctly demonstrates the use of a using statement to manage a disposable resource in C#?

easy📝 Syntax Q3 of 15
C Sharp (C#) - Exception Handling
Which of the following correctly demonstrates the use of a using statement to manage a disposable resource in C#?
Ausing var resource = new Resource(); resource.DoWork();
Busing (var resource = new Resource()) { resource.DoWork(); }
Cusing Resource resource = new Resource(); resource.DoWork();
Dusing (Resource resource = new Resource()) resource.DoWork();
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct syntax

    The classic using statement requires parentheses around the resource declaration and a block of code.
  2. Step 2: Analyze options

    using (var resource = new Resource()) { resource.DoWork(); } uses the correct syntax: using (var resource = new Resource()) { ... }. using var resource = new Resource(); resource.DoWork(); is valid syntax in C# 8.0 and later, so it is also correct. using Resource resource = new Resource(); resource.DoWork(); omits parentheses and uses incorrect syntax. using (Resource resource = new Resource()) resource.DoWork(); lacks braces which is allowed only if the statement is a single line, but the resource declaration must be inside parentheses.
  3. Final Answer:

    using (var resource = new Resource()) { resource.DoWork(); } -> Option B
  4. Quick Check:

    Classic using requires parentheses and braces [OK]
Quick Trick: Classic using needs parentheses and braces [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses in using statement
  • Not enclosing code block in braces
  • Using incorrect variable declaration syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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