Bird
0
0

Which of the following is the correct way to use this to refer to a class field when a method parameter has the same name?

easy📝 Syntax Q12 of 15
C Sharp (C#) - Classes and Objects
Which of the following is the correct way to use this to refer to a class field when a method parameter has the same name?
Apublic void SetName(string name) { this.name = name; }
Bpublic void SetName(string name) { name = name; }
Cpublic void SetName(string name) { name = this.name; }
Dpublic void SetName(string name) { SetName = name; }
Step-by-Step Solution
Solution:
  1. Step 1: Identify the naming conflict

    The parameter name hides the class field name.
  2. Step 2: Use this to refer to the field

    this.name = name; assigns the parameter value to the class field.
  3. Final Answer:

    public void SetName(string name) { this.name = name; } -> Option A
  4. Quick Check:

    Use this to access fields with same name [OK]
Quick Trick: Use this.field to avoid name conflicts [OK]
Common Mistakes:
MISTAKES
  • Assigning parameter to itself
  • Reversing assignment order
  • Using invalid syntax for assignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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