The this Keyword
In this page:
The this Keyword
this refers to the current object instance inside a non-static method or constructor. It's most often used to disambiguate an instance field from a parameter of the same name. this can also be used to call another constructor on the same class.
Note: Use this.fieldName = fieldName; in a constructor when the parameter shares the field's name.
Example: The this Keyword
using System;
class Rectangle
{
public int Width;
public int Height;
public Rectangle(int width, int height)
{
this.Width = width;
this.Height = height;
}
public int Area()
{
return this.Width * this.Height;
}
}
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(4, 5);
Console.WriteLine(r.Area());
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: