0

facade

In C# there is concept of static and instance variables.

Static variables are shared by all instances.

In the code snapshot from above, an instance field _bigClass is created. In the constructor and other methods, this is instantiated and it's methods are invoked. My question is - why has the user not used this._bigClass? I'm trying to understand whether it is not necessary to use the this. keyword to access variables within c# class?

variable
  • 8,262
  • 9
  • 95
  • 215

2 Answers2

3

Simply because you do not need to.

If there is a member _bigClass (and there is not a local variable _bigClass whose name would "shadow" it), then just writing _bigClass is enough.

Writing this._bigClass would be the same.

That's it.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
3

In your case it's not required or necessary since that variable is not marked as static, compiler can identify that it's a instance member and not a static one.

In other case, where your instance variable and constructor variable names are same, you can use this to specify the instance member explicitly like

public ClassA(int x, int y)
{
    this.x = x;
    this.y = y;
}   
Rahul
  • 76,197
  • 13
  • 71
  • 125