The this keyword helps you refer to the current object inside its own class. It makes your code clear and avoids confusion when names overlap.
This keyword behavior in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
this.memberNamethis always refers to the current instance of the class.
You can use this to call fields, properties, methods, or constructors.
Examples
this to distinguish between the field and the parameter with the same name.C Sharp (C#)
class Person { private string name; public Person(string name) { this.name = name; // 'this.name' is the field, 'name' is the parameter } public void ShowName() { Console.WriteLine(this.name); // Access current object's field } }
this to call one constructor from another in the same class.C Sharp (C#)
class Counter { private int count; public Counter() : this(0) { } // Calls another constructor public Counter(int start) { this.count = start; } }
this can be used to refer to the current object itself.C Sharp (C#)
class Example { public void PrintSelf() { Console.WriteLine(this); // Prints the current object reference } }
Sample Program
This program creates a Person object with the name "Alice". It uses this to set and access the name field and to print the current object reference.
C Sharp (C#)
using System; class Person { private string name; public Person(string name) { this.name = name; // Use 'this' to refer to the field } public void Greet() { Console.WriteLine($"Hello, my name is {this.name}."); } public void ShowThis() { Console.WriteLine(this); // Shows the object reference } } class Program { static void Main() { Person p = new Person("Alice"); p.Greet(); p.ShowThis(); } }
Important Notes
When you print this directly, it shows the class name unless you override ToString().
You don't always need to use this if there is no naming conflict, but it can improve clarity.
Summary
this refers to the current object inside its class.
Use this to avoid confusion when names overlap.
this can call other constructors or pass the current object.
Practice
1. What does the
this keyword refer to inside a class in C#?easy
Solution
Step 1: Understand the role of
Thethisthiskeyword always points to the current object instance inside its class.Step 2: Differentiate from other options
It does not refer to static methods, local variables, or base classes.Final Answer:
The current instance of the class -> Option DQuick Check:
this= current object [OK]
Hint: Remember:
this means current object instance [OK]Common Mistakes:
- Confusing
thiswith static members - Thinking
thisrefers to base class - Assuming
thisis a local variable
2. 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
Solution
Step 1: Identify the naming conflict
The parameternamehides the class fieldname.Step 2: Use
thisto refer to the fieldthis.name = name;assigns the parameter value to the class field.Final Answer:
public void SetName(string name) { this.name = name; } -> Option AQuick Check:
Usethisto access fields with same name [OK]
Hint: Use
this.field to avoid name conflicts [OK]Common Mistakes:
- Assigning parameter to itself
- Reversing assignment order
- Using invalid syntax for assignment
3. What will be the output of the following code?
class Person {
public string Name;
public Person(string Name) {
this.Name = Name;
}
public void PrintName() {
Console.WriteLine(this.Name);
}
}
var p = new Person("Alice");
p.PrintName();medium
Solution
Step 1: Analyze constructor assignment
The constructor assigns the parameterNameto the fieldNameusingthis.Name = Name;.Step 2: Check output of PrintName()
The method prints the fieldName, which holds "Alice".Final Answer:
Alice -> Option CQuick Check:
Constructor sets field, print shows "Alice" [OK]
Hint: Constructor sets field with
this, print shows value [OK]Common Mistakes:
- Confusing parameter and field values
- Expecting default null output
- Thinking
thiscauses error
4. Identify the error in this code snippet:
class Car {
public string Model;
public Car(string Model) {
Model = Model;
}
}medium
Solution
Step 1: Understand assignment in constructor
The assignmentModel = Model;assigns the parameter to itself, not the field.Step 2: Use
Usingthisto fixthis.Model = Model;assigns the parameter value to the field.Final Answer:
Missingthiskeyword causes field not to be set -> Option AQuick Check:
Usethisto assign fields when names overlap [OK]
Hint: Assign fields with
this.field = param to avoid errors [OK]Common Mistakes:
- Assuming parameter assigns field automatically
- Thinking constructor syntax is wrong
- Believing parameter names must differ from fields
5. Consider this class with two constructors:
What will be the output and why?
class Box {
public int Width, Height;
public Box() : this(10, 20) {}
public Box(int Width, int Height) {
this.Width = Width;
this.Height = Height;
}
}
var b = new Box();
Console.WriteLine($"{b.Width}, {b.Height}");What will be the output and why?
hard
Solution
Step 1: Understand constructor chaining with
The parameterless constructor calls the two-parameter constructor with values 10 and 20.thisStep 2: Check field initialization
The two-parameter constructor setsWidthandHeightto 10 and 20 respectively.Final Answer:
10, 20 because the parameterless constructor calls the other constructor usingthis-> Option BQuick Check:
Constructor chaining withthissets fields [OK]
Hint: Use
this(args) to call another constructor [OK]Common Mistakes:
- Thinking fields remain default zero
- Assuming constructor chaining causes errors
- Confusing
thiswith base constructor calls
