使用'this':哪里好,哪里不好

aba*_*hev 0 .net c# coding-style this

我喜欢对所有非局部变量使用'this'语句:对于属性,类变量等,我这样做是为了方便阅读代码,易于理解这个变量的来源.

object someVar;

object SomeProperty { get; set }

void SomeMethod(object arg1, object arg2)
{
  this.SomeProperty = arg1;
  this.someVar = arg2;
}
Run Code Online (Sandbox Code Playgroud)

您如何看待,使用'this'的正确方法是什么?

cor*_*ore 6

this是一件好事.this消歧.

O_O:

public class Foo
{
    private string bar

    public Foo(string bar)
    {
        bar = bar;
    }
}
Run Code Online (Sandbox Code Playgroud)

下划线是蹩脚的(它是C#,而不是C++):

public class Foo
{
    private string _bar

    public Foo(string bar)
    {
        _bar = bar;
    }
}
Run Code Online (Sandbox Code Playgroud)

好极了:

public class Foo
{
    private string bar

    public Foo(string bar)
    {
        this.bar = bar;
    }
}
Run Code Online (Sandbox Code Playgroud)