What is the meaning of "this" in C #

Can someone explain the meaning of "this" in C #?

For instance:

// complex.cs using System; public struct Complex { public int real; public int imaginary; public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } 
+4
source share
7 answers

The keyword this is a reference to the current instance of the class.

In your example, this used to refer to the current instance of the Complex class and it removes the ambiguity between int real in the constructor signature and public int real; in the class definition.

MSDN has some documentation that is also worth checking out.

Although this is not directly related to your question, another use of this used as the first parameter in extension methods. It is used as the first parameter that designates the instance to use. If you need to add a method to a String class , you can simply write any static class

 public static string Left(this string input, int length) { // maybe do some error checking if you actually use this return input.Substring(0, length); } 

See also: http://msdn.microsoft.com/en-us/library/bb383977.aspx

+14
source

This keyword refers to the current instance of the class and is also used as a modifier of the first parameter of the extension method.

enter image description here

this (C # link) - MSDN
C # keywords - MSDN

+2
source

this refers to an instance of the class.

+1
source

Nate and d_r_w have an answer. I just want to add this to my code exactly to this. in fact refers to a member of the CLASS to distinguish from the arguments of the FUNCTION. So the line

 this.real = real 

means the assignment of the function value (in this case, the constructor) to the "real" parameter for a member of the "real" class. In the general case, you would also use the case to make the difference clearer:

 public struct Complex { public int Real; public int Imaginary; public Complex(int real, int imaginary) { this.Real = real; this.Imaginary = imaginary; } } 
+1
source

When the body of the method

 public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } 
Performed

it runs on a specific instance of struct Complex . You can access the instance executed by the code using the this . Therefore you can think of the body of the method

 public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } 

like reading

 public Complex(int real, int imaginary) { assign the parameter real to the field real for this instance assign the parameter imaginary to the field imaginary for this instance } 

There is always an implicit this , so the following equivalents

 class Foo { int foo; public Foo() { foo = 17; } } class Foo { int foo; public Foo() { this.foo = 17; } } 

However, local residents have priority over members, therefore

 class Foo { int foo; public Foo(int foo) { foo = 17; } } 

assigns 17 , so the variable foo is a parameter of the method. If you want to assign an instance member, if you have a method where there is a local name with the same name, you should use this to refer to it.

+1
source

This is a variable representing the current instance of the class. for instance

 class SampleClass { public SampleClass(someclass obj) { obj.sample = this; } } 

In this example, this is used to set the "sample" property to someclass obj, on the current instance of SampleClass.

0
source

Refers to the current instance of the class

0
source

All Articles