-1

The older apprentices in my company use "this." a lot. Two weeks ago I started coding object-oriented and still don't get for what it is being used.

  • 1
    It means _this_ instance. If you are in a static method there is no instance, so you can't use it. – Tim Schmelter Oct 24 '17 at 07:43
  • That's not really a duplicate, OP doesn't even understand for what the word is used, he's not asking if it's better to use/omit it. – Tim Schmelter Oct 24 '17 at 07:45
  • Here's the answer, https://imgur.com/ywS3UDp. Sorry but I cannot reopen a wrongfully closed question all by myself – Eric Herlitz Oct 24 '17 at 07:50
  • IMHO you should use it when it only requires. Some people just overuse 'this' because they just want to get local variables list from intellisense. – Renatas M. Oct 24 '17 at 07:57
  • For information on what `this` means see the C# reference https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/this – Liam Oct 24 '17 at 08:34

3 Answers3

2

You need to understand what instance is first. Let's say you have an object:

public class House
{
    public decimal Height { get; set; }
}

You can have multiple instances of it:

var smallHouse = new House { Height = 100M };
var bigHouse = new House { Height = 300M };

Each instance has its own value of Height. When you want to work with Height in a method of House, you need to refer to the current instance method is operating at (the one consumer called).

This can be done explicitly by using this as a special kind of variable that refers to this current instance:

public class House
{
    public decimal Height { get; set; }

    public bool IsItTooBig()
    {
        return this.Height > 200;
    }
}

Or you can omit this and let C# guess that what you mean is the instance value:

public class House
{
    public decimal Height { get; set; }

    public bool IsItTooBig()
    {
        return Height > 200;
    }
}

Programmers differ in opinion whether it's good or bad to be explicit there. If you follow capitalization conventions, you can distinguish instance state and method scope state (normal variables) by it.

There are cases where you absolutely need it, for example when you have naming conflict, or when you want to return current instance from a method:

public class House
{
    public decimal Height { get; set; }

    public House AddFloor()
    {
        Height += 100;
        return this;
    }
}

You should consider applying immutability in many of these cases though.

Lukáš Lánský
  • 4,641
  • 3
  • 32
  • 48
0

The keyword 'this' represents the instance of an object used to explicitly call a method, field or property of that instance.

Commonly used when your private fields have the same name as the parameters in a given method:

private string name;

public void SetName(string name) {
    this.name = name;
}
Folkert
  • 52
  • 8
0

When you want to refer to instance field within that class you use this, it can be omitted but there are cases it can not be omitted.

public class InstanceClass
{
    int field = 10;
    public void Method()
    {
        int field = 0;

        Console.WriteLine(field); //      outputs 0
        Console.WriteLine(this.field); // outputs 10 because "this" refers to field.
    }
}

if there is no declared local variable that conflicts with field name, "this" can be omitted.

public class InstanceClass
{
    int _field = 10;
    public void Method()
    {
        int field = 0;

        Console.WriteLine(field); 
        Console.WriteLine(_field); // prefixed with _.  
                                   // no conflicts
                                   // so "this" can be omitted.
    }
}

another case where you can not omit this, is when you use indexer.

public class InstanceClass
{
    private List<int> _source;
    private int offset;

    public int this[int index] // you use "this"
    {
        get => _source[index + offset]
        set => _source[index + offset] = value;
    }

    public void Method()
    {
        var first = this[0]; // must use "this" to refer to indexer for this class.
    }
}

"this" is used for calling constructor overloads too.

public class Foo
{
    public Foo() : this(0) 
    {
        Console.WriteLine("world");
    }

    public Foo(int param1)
    {
        Console.WriteLine("Hello");
    }
}

//...

var foo = new Foo(); // outputs "Hello world"

"this" also refers to instance of class itself. so if you want to return instance of self you use this.

public class Foo
{
    public Foo ReturnMe() // weird example.
    {
        return this;
    }
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118