2
public string GetTitle()
{
    return title;
}

As you can see in the above code, in the GetTitle() method, it is being used to return the title instance variable. My question is, what would be the difference if I added the 'this' keyword such that the GetTitle() method becomes as shown below:

public string GetTitle()
{
    return this.title;
}

I haven't seen an example in a book or anywhere that does the latter. But I tried this in Visual Studio, and it still does the same thing.

Please can someone explain what the difference is, if any?

Thank you! :)

Christos
  • 53,228
  • 8
  • 76
  • 108
Baba.S
  • 324
  • 2
  • 14

6 Answers6

4

Please can someone explain what the difference is, if any?

There isn't any difference between:

public string GetTitle()
{
    return title;
}

and this:

public string GetTitle()
{
    return this.title;
}

The this refers to the instance of the class. So you can access the field either as title or as this.title.

However, I don't see any in following the above approach for defining a getter and a setter. Instead of following the above approach, you could use the following syntax, in order to achieved the same thing:

public string Title { get; set; }

and whenever you want to read the Title, you just use the following

classInstance.Title

and whenver you want to set it's value, you just use the following:

classInstance.Title = "your title";

In both cases classInstance as it's name implies is a n instance of your class.

It is remarkable to state here that the above is called auto-implemented properties. Before this, the only way you could do the same, it would be the following:

private string title;
public string Title
{
    get { return title; }
    set { title = value; }
}

The following syntax is used nowadays, whenever you want to apply any logic inside the get or set. If it isn't this the case, then the auto-implemented property approach is usually followed, since it is more compact.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • A comprehensive answer. I add that the use of an explicit backing variable rather than auto-get/set is that you can assign a default value using an explicit backing variable. Example: private string title = ""; in your last code block will prevent NullReferenceException, and an auto-get/set cannot (until the very latest C# version) have a default value assigned. For this reason I recommend the use of explicit backing variables; and consequently "this." for readability. – PhillipH Aug 07 '16 at 16:29
1
public void SetTitle(string title)
{
 this.title = title;
}

The this is only needed in this case because the membervariable and the parameter have the same name.

It would not make any difference if with or without this if the parameter was called intitle or any other name than the membervariable.

Tomas Dittmann
  • 424
  • 7
  • 18
1

this (C# Reference)

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.

That said it makes sure that the field referenced is local to the current instance.

In your particular context they will mean the same thing provided the field/variable is local.

If the field was static then following example would fail

static string title;
public string GetTitle() {
    return this.title;
}

this would still compile but all instance would be referencing the same static field

static string title;
public string GetTitle() {
    return title;
}

it is commonly used to qualify local members hidden by similar names, for example:

public void SetTitle(string title) {
    this.title = title;
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

Actually, you can use it or avoid, There is no difference in the context of properties. Because When we are using this properties then we also use the variable too using the same instance.

If you use Resharper or something like that application then it suggests you to use it. Basically its more safer to use this keyword and also best practice. Other than there is no difference of it

There are several use of this keyword you can take a look at this answer here

Community
  • 1
  • 1
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
0
public string GetTitle()
    {
        return this.title;
    }

There are no differences, adding the "this" keyword is not necessary but you can add it if It's more clear for you..It's just more explicit.

  • -

    public string GetTitle() { return title; }

    public void SetTitle(string title)
    {
        this.title = title;
    }
    

In the SetTitle(string title) method is necessary adding the "this" keyword because you need to distinguish which "title" variable you are changing. By adding "this" you are referring to the object "title" variable, not the parameter.

0

Code 1:

 public string GetTitle()
    {
        return title;
    }

Code 2:

  public string GetTitle()
    {
        return this.title;
    }

There isn't any difference in code 1 and 2.

Both codes have same output. One important thing i want to share is that this contain the reference of object that is being used to calls this GetTitle()

Usman lqbal
  • 935
  • 1
  • 9
  • 31