我一直在使用实例成员时,在其前面加上
this.
,在使用静态成员时,在其前面加上类型名称,以便清晰明确。然而Roslyn似乎不赞同这种写法,并礼貌地建议可以在适当的情况下省略this.
和Type.
...所以当我会写this.时...(没有双关语意味)public void DoSomethingCool()
{
this.CallAwesomeMethod();
CoolCucumber.DoSomethingLessAewsome();
}
…Roslyn建议我这样做…
public void DoSomethingCool()
{
CallAwesomeMethod();
DoSomethingLessAwesome();
}
然而,当涉及到使用扩展方法时,似乎我不能省略使用this.
,例如...
public int GetTotal()
{
// This doesn't work
return Sum(o => o.Value);
// This does
return this.Sum(o => o.Value);
}
问题:
- 为什么Roslyn似乎不喜欢显式使用
this.
和Type.
? - 为什么我需要显式使用扩展方法的
this.
? - 虽然这不是这个问题的严格部分,但我该如何解决Roslyn不希望我使用
this.
和Type.
,而StyleCop的分析器坚持要求我使用它们之间的差异?
this
关键字可能会出现问题:https://dev59.com/A14c5IYBdhLWcg3weaT8#27884101 - Jeroen Vannevelthis
也应该被省略。 - Jeroen Vannevel