Is that correct using of "this" keyword? [duplicate]
I would like to know if I get it correctly: also with this keyword I can distinct between fields and variables? Like this:
class X
{
int x;
public X(int x)
{
this.x=x;
}
}
Yes, if a method parameter (or local variable) has the same name as a field, you need to use this
to distinguish the two. Also, StyleCop is very vocal about every class member access being done through this
, but whether that's a good idea or not may be up to debate. It makes things more clear, but also adds much visual clutter.
精彩评论