In C #, the current properties of an object are accessed through this. XYZ is considered bad style compared to just XYZ

Is it a simple case to just not use this.XYZ construct?

+1


source to share


5 answers


He is considered only bad style if he breaks your style rules. Sometimes the usage is this

necessary to qualify a member variable against a local variable:

public MyType(int arg)
{
    this.arg = arg;
}

      



This problem can also be mitigated with style guides. For example, prefix members with "_":

public MyType(int arg)
{
    _arg = arg;
}

      

+6


source


I wouldn't say it's bad style - but it's not particularly idiomatic.

My almost only use this.foo

is for copying parameters to fields:



public Person (string name, string occupation)
{
   this.name = name;
   this.occupation = occupation;
}

      

+1


source


I've never heard of it being a style issue. I used this all the time to get intellisense, but then I started using ctrl-j, after which I just remembered my object properties without using a crutch.

Probably because my objects have become less complex as I gain more experience ...

+1


source


I always use this. for a global variable. This way I can clearly know that I am using a global variable without using the "_" type prefix.

+1


source


MS StyleCop tool insists on this .XYZ (or should be this.Xyz) option when parsing source code.

+1


source







All Articles