this in unity

62

this in unity -

The “this” keyword in C# is used to refer to the current instance of the class.
It is also used to differentiate between the method parameters and class fields
if they both have the same name

Example code:

using System;
 
namespace ThisKeyword {
  class Test {

    int num;
    Test(int num) {
      
      // this.num refers to the instance field
      this.num = num;
    }

    static void Main(string[] args) {

      Test t1 = new Test(4);
      Console.WriteLine("value of num: " +t1.num);
      Console.ReadLine();
    }
  }
}

// output:  value of num: 4

Comments

Submit
0 Comments