0
0
C Sharp (C#)programming~5 mins

This keyword behavior in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the this keyword refer to in C#?
The <code>this</code> keyword refers to the current instance of the class where it is used. It helps access members (fields, methods) of that instance.
Click to reveal answer
beginner
How can this be used to resolve naming conflicts in constructors?
When constructor parameters have the same names as class fields, <code>this</code> is used to refer to the class fields, distinguishing them from parameters.
Click to reveal answer
beginner
Can this be used in static methods? Why or why not?
No, <code>this</code> cannot be used in static methods because static methods belong to the class itself, not to any instance. <code>this</code> refers to an instance.
Click to reveal answer
intermediate
What is the purpose of using this in extension methods?
In extension methods, this before the first parameter indicates which type the method extends, allowing the method to be called as if it were part of that type.
Click to reveal answer
intermediate
Explain how this can be used to call another constructor in the same class.
Using this(...) in the constructor initializer calls another constructor in the same class, allowing code reuse and cleaner initialization.
Click to reveal answer
What does this refer to inside a non-static method?
AThe current instance of the class
BThe class itself
CA static member
DA local variable
Can you use this inside a static method?
AYes, always
BOnly if the method is private
CNo, because static methods have no instance
DOnly if the method is public
How do you call another constructor in the same class using this?
Athis(); inside the constructor body
Bbase(); inside the constructor body
Csuper(); in the constructor initializer
Dthis(...); in the constructor initializer
In extension methods, what does the this keyword before the first parameter mean?
AIt marks the method as static
BIt specifies the type being extended
CIt makes the method private
DIt creates a new instance
Why use this to refer to fields when constructor parameters have the same name?
ATo distinguish class fields from parameters
BTo access static members
CTo call base class constructor
DTo create new objects
Explain the role of the this keyword in C# and give two examples of its use.
Think about how <code>this</code> helps inside instance methods and constructors.
You got /5 concepts.
    Why can't this be used inside static methods? What does this tell you about static methods?
    Consider the difference between instance and static members.
    You got /3 concepts.