Challenge - 5 Problems
This Keyword Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of 'this' in instance method
What is the output of this C# program?
C Sharp (C#)
class Person { public string Name; public Person(string name) { this.Name = name; } public void PrintName() { System.Console.WriteLine(this.Name); } } var p = new Person("Alice"); p.PrintName();
Attempts:
2 left
💡 Hint
Remember 'this' refers to the current object instance.
✗ Incorrect
The this keyword inside an instance method refers to the current object. Here, this.Name accesses the Name field of the Person instance, which was set to "Alice".
❓ Predict Output
intermediate2:00remaining
Using 'this' in static method
What happens when you try to compile and run this C# code?
C Sharp (C#)
class Sample { public static void Show() { System.Console.WriteLine(this.ToString()); } } Sample.Show();
Attempts:
2 left
💡 Hint
Static methods do not have an instance context.
✗ Incorrect
The this keyword cannot be used inside static methods because there is no instance of the class to refer to. This causes a compilation error.
❓ Predict Output
advanced2:00remaining
Output of 'this' in struct method
What is the output of this C# program?
C Sharp (C#)
struct Point {
public int X, Y;
public Point(int x, int y) {
this.X = x;
this.Y = y;
}
public void Move(int dx, int dy) {
this.X += dx;
this.Y += dy;
System.Console.WriteLine($"{this.X},{this.Y}");
}
}
var p = new Point(1, 2);
p.Move(3, 4);Attempts:
2 left
💡 Hint
Structs are value types but can use 'this' inside instance methods.
✗ Incorrect
In a struct, this refers to the current value. The Move method updates the fields and prints the new coordinates.
❓ Predict Output
advanced2:00remaining
Output of 'this' in constructor chaining
What is the output of this C# program?
C Sharp (C#)
class Box { public int Size; public Box() : this(10) { System.Console.WriteLine(this.Size); } public Box(int size) { this.Size = size; System.Console.WriteLine(this.Size); } } var b = new Box();
Attempts:
2 left
💡 Hint
Constructor chaining calls the parameterized constructor first.
✗ Incorrect
The parameterized constructor sets Size to 10 and prints it. Then the parameterless constructor prints this.Size which is still 10.
🧠 Conceptual
expert2:00remaining
Behavior of 'this' in extension methods
In C#, what does the
this keyword mean in the parameter of an extension method?Attempts:
2 left
💡 Hint
Extension methods add behavior to existing types by using 'this' in the first parameter.
✗ Incorrect
In extension methods, this before the first parameter indicates the method extends that type, and inside the method, it refers to the instance of that type.