this keyword

In this chapter you will learn:

  1. How to use this keyword to reference current type
  2. How to use this keyword to avoid name hiding
  3. Invoke a constructor through this

Using this keyword

this is referencing the current type.

using System;//from   j a v a  2 s  . c  om
class Rectangle {
   public int Width;
   public int Height;
   public Rectangle(int w = 5, int h = 6){
     this.Width = w;
     this.Height = h;
   }
}

class Program
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle();
        Console.WriteLine(r.Width);
        Console.WriteLine(r.Height);
    }
}

The output:

this and name shading

this can be used to avoid name shading.

using System;/*j  a  va 2 s.  com*/
class Rectangle {
   public int Width;
   public int Height;
   public Rectangle(int Width, int Height){
     this.Width = Width;
     this.Height = Height;
   }
}

class Program
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle(2,3);
        Console.WriteLine(r.Width);
        Console.WriteLine(r.Height);
    }
}

The output:

this cannot be used in static context.

this calls constructors

The general form using this to call constructors is shown here:

constructor-name(parameter-list1) : this(parameter-list2) {
   // ... body of constructor, which may be empty
}

The following code shows how to use this keyword to call constructors.

using System;  //j a v a2s.  co m
  
class XYCoord {   
  public int x, y;   
   
  public XYCoord() : this(0, 0) { 
    Console.WriteLine("Inside XYCoord()"); 
  }  
 
  public XYCoord(XYCoord obj) : this(obj.x, obj.y) { 
    Console.WriteLine("Inside XYCoord(obj)"); 
  }  
 
  public XYCoord(int i, int j) {  
    Console.WriteLine("Inside XYCoord(int, int)"); 
    x = i; 
    y = j; 
  }     
}     
     
class MainClass {     
  public static void Main() {     
    XYCoord t1 = new XYCoord();   
    XYCoord t2 = new XYCoord(8, 9);   
    XYCoord t3 = new XYCoord(t2);   
   
    Console.WriteLine("t1.x, t1.y: " + t1.x + ", " + t1.y);  
    Console.WriteLine("t2.x, t2.y: " + t2.x + ", " + t2.y);  
    Console.WriteLine("t3.x, t3.y: " + t3.x + ", " + t3.y);  
  }     
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Using base to Access a Hidden Name
  2. Call constructor in base class
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances