C# This

The this keyword stands for the current instance or object of the class. It can also be used in indexers or extension methods which will be introduced later.

this can eliminate naming conflicts between field names and parameter names. If a parameter name X is the same as a field name X, X always refers to the parameter name. Or we say the local name has higher priority to be accessed. In this case, we'll use this.X to refer to the field name.

Example 01-51-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;

namespace TestThis1
{
    public class Person
    {
        public string name;

        public Person()
        {
            name = "None";
        }

        public Person(string name)
        {
            this.name = name;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person a = new Person();
            Console.WriteLine("The name of the person is {0}.", a.name);

            Person b = new Person("John");
            Console.WriteLine("The name of the person is {0}.", b.name);
            Console.Read();
        }
    }
}

Output

The name of the person is None.
The name of the person is John.

Explanation

  • Line 5-18: Define a public class Person.
  • Line 7: Declare a string field name.
  • Line 9-12: Define the default constructor.
  • Line 11: The "name" in this line stands for the field "name".
  • Line 14-17: Define a constructor with one parameter. The parameter is a string type and its name is the same as the field's.
  • Line 16: "name" here is the parameter and this.name is the field. In this line, the value of the parameter name is assigned to the field. this.name stands for the field of the current instance.
  • Line 20-31: Define a class Program for testing.
  • Line 22: Main method starts here.
  • Line 24: Instantiate Person by calling the default constructor. In the constructor, the value of the field is set to "None".
  • Line 25: Output the string.
  • Line 27: Create an instance and transfer a argument "John" to the constructor in line 14-17.
  • Line 28: Output the person's name.

Example 01-51-02

This is the revision of the example 01-51-01.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;

namespace TestThis2
{
    public class Person
    {
        public string name;

        public Person() : this("None")
        {
        }

        public Person(string name)
        {
            this.name = name;
            this.outputName();
        }

        public void outputName()
        {
            Console.WriteLine("The name of the person is {0}.", this.name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person a = new Person();
            Person b = new Person("John");
            Console.Read();
        }
    }
}

Output

The name of the person is None.
The name of the person is John.

Explanation

  • Line 5-23: Define a public class Person.
  • Line 7: Declare a string field name.
  • Line 9-11: Define the default constructor by calling this("None"). We know this refers to the current instance. this("None") means Person("None") so it is calling another constructor with a parameter defined in line 13-17.
  • Line 13-17: Define a constructor with one parameter. This constructor was called by this("None") above.
  • Line 15: The parameter's value is assigned to the field.
  • Line 16: Call the method outputName(). In this line, "this" can be omitted.
  • Line 19-22: Define a method to output a string including the value of the field "name".
  • Line 25-33: Define a class Program for testing.
  • Line 27: Main method starts here.
  • Line 29: Call the default constructor to create an instance of class Person. "None" is assigned to the field "name". At last, output a string by calling the method in line 16.
  • Line 30: Call the second constructor and transfer an argument to the field. Then output the result with the field's value.

Example 01-51-03

In this example, we'll use this as an argument to transfer the current instance to a method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;

namespace TestThis3
{
    public class Person
    {
        public string name;

        public Person() : this("None")
        {
        }

        public Person(string name)
        {
            this.name = name;
            this.outputName();
        }

        public void outputName()
        {
            Console.WriteLine("The name of the person is {0}.", this.name);
            Group g = new Group();
            g.add(this);
        }
    }

    public class Group
    {
        public void add(Person p)
        {
            Console.WriteLine("{0} joins the group.", p.name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person a = new Person();
            Person b = new Person("John");
            Console.Read();
        }
    }
}

Output

The name of the person is None.
None joins the group.
The name of the person is John.
John joins the group.
  • Line 5-25: Define a public class Person as the previous example.
  • Line 22-23: Create an instance of Group c and run the method "add" of the instance. Here we use "this" to transfer the current instance of the class Person.
  • Line 27-33: Define another public class Group.
  • Line 29: Define a method "add" and the parameter is the class Person. It matches what it is invoked in line 23.
  • Line 35-43: Class Program is created for testing.
  • Line 37-43: Main method is defined here, which is the same as the previous example.