Zach Smith
Zach Smith

Reputation: 8961

Why does Visual Studio prompt me NOT to use the `this` keyword for instance variables?

With a class as defined below, I would expect that I would need to explicitly state instance variables by prefixing them with 'this'. Coming from a Ruby and Javascript background, I was expecting that description would need to be prefixed with this at the declaration, and that within the constructor, that this would be required.

Why is it not required? I assume that description is still getting created as an instance variable?

public class Item
{
    private string description;
    public Item(string str)
    {
        this.description = str; // VS says the 'this' keyword can be omitted
    }
}

Upvotes: 2

Views: 440

Answers (4)

Sefe
Sefe

Reputation: 14007

Here are some excerpts from the C# Language Specification, which is the authorative source for how C# works (emphasis added):

§3.7: The scope of a member declared by a class-member-declaration (§10.1.6) is the class-body in which the declaration occurs.

That means you can access the Item.description field by its name from all instance methods of Item. Because the variable is in scope, you don't need this.

§3.7: The scope of a local variable declared in a local-variable-declaration (§8.5.1) is the block in which the declaration occurs.

That means you could access a local variable description within the block it's defined in.

§3.7.1.1 Name hiding through nesting can occur as a result of nesting namespaces or types within namespaces, as a result of nesting types within classes or structs, and as a result of parameter and local variable declarations.

That means that if you define both Item.description and a local description. In this case the local variable hides the instance field.

§7.6.7 When this is used in a primary-expression within an instance method or instance accessor of a class, it is classified as a value. The type of the value is the instance type (§10.3.1) of the class within which the usage occurs, and the value is a reference to the object for which the method or accessor was invoked.

That means, that this in C# is not a required keyword to specify instance scope. It simply is a value that equals the object the method is called on. That means that this.description is semantically equivalent to anyItem.description as it means accessing the description field on the object.

Putting everything together:

When Item.description is hidden, using this is the only way of accessing the hidden member. However, if it is not hidden, you can access it because it is in scope. Hence, the VS recommends to drop the this, because it's not needed.

Upvotes: 0

Martijn
Martijn

Reputation: 12092

You're asking two different questions.

The first one is, why don't you need this.

The reason for that is that C# is a statically typed language. In javascript, you can say foo = 7, and a variable foo will start to exist.

In C#, you can't do that. description refers to this.description, and in this context can't refer to anything else.

Your second question is why does Visual Studio tell you not to do that. The answer to that is that notifying you is its default code style.

You can change this behaviour in your editor settings. You can choose what you want your codestyle to be (prefer this, prefer not this, and either inform or warn when you do something else).

There is nothing inherently wrong in preferring one style over the other, but it's a good idea to be consistent. Visual Studio provides you the tools to be consistent in the style you choose.

Upvotes: 2

Andrei Dragotoniu
Andrei Dragotoniu

Reputation: 6335

It is definitely not required, but it is correct so in the end is up to you if you want to use it or not.

I use it mainly because it's an indication that whatever property or method it's used it, I know it's inside that class so there is no ambiguity. Both VS and Resharper will pick up on that but I would say it's a personal preference whether you want to use it or not.

Upvotes: 0

Igor
Igor

Reputation: 62213

Because there is no conflicting locally scoped description variable and in that case the type or instance member is used.

Priority order

  1. Local variable
  2. Instance member
  3. Type (static) member

So if you had a local variable also named description but you wanted to reference the instance member then this would be required otherwise you would always reference the the local variable.

Here is an example where you should use this

public class Item
{
    private string description;
    public void SetDescription(string description)
    {
        this.description = description; // without this you would just be setting the local variable to itself
    }
}

Upvotes: 5

Related Questions