C# Reference ยท Keywords and Statements
The this and base keywords
Reference for C# this and base: the current instance, constructor chaining with this(...) and base(...), calling parent members, indexers and extension methods.
this is a reference to the current object inside an instance method, property, indexer, or constructor. You use it to tell a field from a parameter with the same name, to pass the current object to another method, and to chain constructors. base is the same object viewed as its parent class: it lets you call the parent's constructor and the parent's version of an overridden or hidden member.
Neither keyword exists in static members, because there is no instance there.
class Item { private string name; public Item(string name) => this.name = name; // this.name is the field }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
this.member // current instance this[index] // declares an indexer: public T this[int i] { ... } Ctor(...) : this(args) { ... } // constructor chaining, same class Ctor(...) : base(args) { ... } // call a parent constructor base.Member(...) // parent implementation base[index] // parent indexer static R M(this T source, ...) // extension method: 'this' on the first parameter
| Use | Description |
|---|---|
this.x |
Refers to the field or property x of the current object. Needed only when a local or parameter has the same name. |
this as a value |
Passes or returns the current instance. In a struct, this is a variable (a ref), in a class it is read-only. |
: this(...) |
Runs another constructor of the same class first, then the rest of this body. |
: base(...) |
Selects which parent constructor runs. Without it, the parent's parameterless constructor is used. |
base.Method() |
A non-virtual call to the parent's implementation. |
Errors: this or base in a static member is CS0026 and CS1511; a parent without an accessible parameterless constructor and no : base(...) call is CS7036; constructors that chain to themselves in a cycle are rejected by the compiler.
Usage notes
- Initialization order. Field initializers of the derived class run first, then the parent constructor, then the derived constructor body. Parent field initializers run before the parent constructor body.
baseis not a variable. You cannot passbasearound or assign it;((Animal)this)gives you a reference of the parent type, but virtual calls on it still go to the override.- Only one level.
base.base.Xdoes not exist. If you need the grandparent's version, restructure or add a helper in the middle class. - Primary constructors (C# 12). When a class declares
class C(int x), other constructors must chain with: this(...). The parent's primary constructor is called asclass D(int x) : C(x). - Extension methods put
thisin front of the first parameter to make it callable like an instance method. - Leaking
this. Passingthisto other code from a constructor hands out a partially built object. Do it only when the receiver will not use it yet. - Style. Skip
this.where there is no ambiguity unless your team convention requires it. - Lambdas capture
thisimplicitly when they use instance members, which keeps the object alive as long as the lambda lives.
Related
- virtual and override
- new
- static
- extension
- Tutorial: Constructors, Inheritance