Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# What makes the difference between the two this keywords in the constructor below?

I don't understand the following: In the code found below, Visual Studio tells me, that I can simplify my code by deleting the second this keyword in the constructor.

But why can't then I do away with the first this keyword? Both variables were declared outside the constructor, in the class, so both will be "overridden"* for the instance.

If I remove both this keywords, then VS will complain that the first assignment is made to same variable, but not for the second. The only obvious difference to me is the second variable is an array, but I do not understand how would that make it any different?

*I suspect override is not the correct term here.

public class CelestialObject {

    CelestialBody[] celestialBodies;
    int celestialBodyCount;

    public CelestialObject(int celestialBodyCount = 2) {
        this.celestialBodyCount = celestialBodyCount;
        this.celestialBodies = new CelestialBody[celestialBodyCount];
    }
}
like image 962
Balinth Avatar asked Dec 24 '22 22:12

Balinth


2 Answers

You cannot do away with the first this keyword because you have a parameter with the same name as your field celestialBodyCount:

int celestialBodyCount; // The field

And

public CelestialObject(int celestialBodyCount = 2) { // The parameter.

The this keyword is required to indicate that the field is being assigned to. Otherwise the parameter takes precedence over the field, as explained in 3.7.1 Name hiding:

... the scope of an entity may include declarations that introduce new declaration spaces containing entities of the same name. Such declarations cause the original entity to become hidden.

And then 3.7.1.1 Hiding through nesting:

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.

In this case the parameter celestialBodyCount hides the member celestialBodyCount.

Of course, if you do remove the this the compiler will helpfully give you the following warning:

Assignment made to same variable; did you mean to assign something else?

This warning almost always indicates a bug and should always be cleaned up.

like image 70
dbc Avatar answered Feb 02 '23 00:02

dbc


The difference is that you've got a parameter called celestialBodyCount - which means that within the constructor, the identifier celestialBodyCount refers to the parameter, so to access the field you have to qualify it with this.

You don't have a parameter (or other local variable) called celestialBodies, so that identifier already refers to the field, without any qualification required.

like image 27
Jon Skeet Avatar answered Feb 01 '23 23:02

Jon Skeet