3

It helps to note I'm very new to c#.

But consider the following class:

class AllItems
{
    private readonly Database database;

    public AllItems(Database database)
    {
        this.database = database;
    }
}

I have the following questions:

  1. In the above example we are assigning private readonly Database databasethe value which gets passed into AllItems class, is that correct?

  2. Would it matter if I swapped this.database = database around, so it looked like database = this.database instead?

  3. Where we are saying this.database, does this refer to the database in AllItems(Database database) or does it refer to private readonly Database database ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Allen S
  • 3,471
  • 4
  • 34
  • 46

4 Answers4

7

In the above example we are assigning private read-only Database database the value which gets passed into the AllItems class, is that correct?

Yes

Would it matter if I swapped this.database = database around, so it looked like database = this.database instead?

Yes, it would. What you would end up doing there is assigning the database parameter being passed in to be the value of the database field (which would be null by default).

Where we are saying this.database, does this refer to the database in AllItems(Database database) or does it refer to private read-only Database database?

this is shorthand for the current class instance, therefore it refers to the private read-only member of database.

karel
  • 5,489
  • 46
  • 45
  • 50
James
  • 80,725
  • 18
  • 167
  • 237
2
  1. Yes
  2. Yes it would matter, then you would assigning the value of this.database (in this case null) into database.
  3. this.database in this case refers to private readonly Database database; field. I suggest you read more about the this keyword here.
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
  • yes, I mean that it refers to that field as opposed to the argument being passed in the constructor. I have corrected my answer – Sebastian Piu Nov 24 '13 at 12:08
1
  1. Yes, it's correct, you can set a readonly field in the class constructor, that's the only place we can initialize readonly fields/properties.

  2. It matters the fact that the swapping does a meaningless thing. When you do this database = this.database, the database is assigned to the value of this.database which is null in this case, but that local variable is not used and will become unavailable outside the constructor.

  3. this.database of course references the private field of your class. In this case the private field and the argument have the same name so we have to use the this to avoid ambiguity.

King King
  • 61,710
  • 16
  • 105
  • 130
1

1) In the above example we are assigning private readonly Database database the value which gets passed into AllItems class, is that correct?

Yes, this is used to refer to objects that are declared in the class.

2) Would it matter if I swapped this.database = database around, so it looked like database = this.database instead?

Yes it would, if you swap it then instead of assigning the value in database declared as parameter in the constructor to the private readonly Database database, it would assign the value in `private readonly Database database to the database declared as a parameter in the constructor.

3) Where we are saying this.database, does this refer to the the the database in AllItems(Database database) or does it refer to private readonly Database database ?

this is a keyword used to refer to variables declared in the class also know as global variables therefore it refers to private readonly Database database.

Also take a look at When do you use the "this" keyword?

Community
  • 1
  • 1