开发者

Is that correct using of "this" keyword? [duplicate]

This question already has a开发者_Python百科nswers here: When do you use the "this" keyword? [closed] (31 answers) In C#, is "this" keyword required? [duplicate] (6 answers) Closed 9 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜