9

Could anyone please explain the meaning "this" in C#?

Such as:

// 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;
   }
razlebe
  • 7,134
  • 6
  • 42
  • 57
user722784
  • 127
  • 1
  • 1
  • 3
  • 5
    why not look up at [MSDN](http://msdn.microsoft.com/en-us/library/dk1507sz.aspx)? "The `this` keyword refers to the current instance of the class." – Vlad Jun 07 '11 at 19:49
  • 8
    vlad - Why not post on stackoverflow ? – JonH Jun 07 '11 at 19:50
  • 1
    @Vlad - A question that's valid for a lot of the questions asked for SO. The point of SO is to be a place that can be used as a reference. Firstly, not everyone knows about `MSDN` (so pointing them there is a good idea). Secondly, the OP has given context. Valid question IMO. – keyboardP Jun 07 '11 at 19:54
  • 3
    It is very difficult to search for common words like "this". Have you actually gone to the MSDN site and typed in "this" to see what the search results are? It is about as difficult as finding out what the ? and ?? mean in if-then statements, or => in a lambda expression. – DOK Jun 07 '11 at 19:54
  • I think the question is valid, though I certainly don't see it hard to find references to `this` via google: `c# this` returns plenty of good results (msdn included). – Chris Walsh Jun 07 '11 at 19:57
  • @DOK: googling "this c#" gives MSDN as the first link. – Vlad Jun 07 '11 at 20:01
  • Sigh. I guess you could pretty much Google anything that we have here at SO. Maybe we should just give up. And especially discourage n00bs. – DOK Jun 07 '11 at 20:28
  • Now that you know what it means, don't use it unless you have to. Unnecessary use of the `this` keyword drives me insane. – codeConcussion Jun 07 '11 at 22:45
  • See MSDN:[this (C# Reference)](http://msdn.microsoft.com/en-us/library/dk1507sz%28VS.80%29.aspx) – Andrew Savinykh Jun 07 '11 at 19:49
  • Possible duplicate of [What is the purpose of 'this' keyword in C#](http://stackoverflow.com/questions/2013857/what-is-the-purpose-of-this-keyword-in-c-sharp) – BlackICE Sep 28 '16 at 04:15
  • Here is the details on Microsoft -[LINK](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this) – Pashyant Srivastava May 17 '21 at 16:34

8 Answers8

20

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

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

MSDN has some documentation on this as well which is worth checking out.

Though not directly related to your question, there is another use of this as the first parameter in extension methods. It is used as the first parameter which signifies the instance to use. If one wanted to add a method to the String class you could simple write in 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

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Nate
  • 30,286
  • 23
  • 113
  • 184
3

When the body of the method

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

is executing, it is executing on a specific instance of the struct Complex. You can refer to the instance that the code is executing on by using the keyword this. Therefore you can think of the body of the method

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

as 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 that the following are equivalent

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

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

However, locals take precedence over members so that

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

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

jason
  • 236,483
  • 35
  • 423
  • 525
1

As most answers are mentioning " the current instance of a class", the word "instance" may be difficult for newbies to understand. "the current instance of a class" means the this.varible is specifically used in the class where it is defined, not anywhere else. Therefore, if the variable name also showed up outside of the class, the developer doesn't need to worry about conflicts/confusions brought by using the same variable name multiple times.

CathyQian
  • 1,081
  • 15
  • 30
1

this references the instance of the class.

Servy
  • 202,030
  • 26
  • 332
  • 449
CassOnMars
  • 6,153
  • 2
  • 32
  • 47
1

Nate and d_r_w have the answer. I just want to add that in your code specifically the this. does in deed refere to the member of the CLASS to distinguish from the arguments to the FUNCTION. So, the line

this.real = real

means assign the value of the function (in this case, constructor) parameter 'real' to the class member 'real'. In general you'd use case as well to make the distinction clearer:

public struct Complex
{
    public int Real;
    public int Imaginary;
    public Complex(int real, int imaginary)
    {
        this.Real = real;
        this.Imaginary = imaginary;
    }
}
n8wrl
  • 19,439
  • 4
  • 63
  • 103
  • 1
    You don't actually need `this` if your argument names differ from field names. But for the OP's example, using `this` is compulsory. – Vlad Jun 07 '11 at 20:04
1

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

enter image description here

this (C# reference) - MSDN
C# Keywords - MSDN

Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
0

this is a variable which represents the current instance of a class. For example

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

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

bbosak
  • 5,353
  • 7
  • 42
  • 60
0

Refers to current instance of class

DeveloperX
  • 4,633
  • 17
  • 22