Alex Rivera | Logout

What is the meaning of "this" in C#

Asked 2011-06-07T19:47:40.917
10

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;
   }
c#
Edit
Report

4 Answers

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

answered 2011-06-07T19:49:00.947
4

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.

answered 2011-06-07T19:58:08.663
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

answered 2011-06-07T20:07:08.803
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.

answered 2011-06-07T19:51:06.663

Your Answer