Jam*_*ood 22 c# visual-studio-2015
所以我有一个非常简单的带有Id字段的类,Id可以在构造函数中设置.
通常,我将使用它this来清楚地标识类属性而不是方法参数.对我来说,这似乎更清晰.
IDE0003要我删除this"姓名可以简化"的消息,这是正确的吗?
这对我来说似乎不太清楚,并且还容易导致套管错误id = id.
mad*_*xej 36
另一个问题的答案是,您可以配置编辑器以删除行为.我个人喜欢"这个"
Tools > Options > Text Editor > C# > Code Style and check Qualify member access with 'this'
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2015 - 更改灯泡,快速操作设置
在this几乎总是关键字是不必要的,看你什么时候使用"this"关键字?.
允许容易导致套管错误
id = id
这将产生另一个警告:
对同一变量进行的赋值; 你的意思是分配其他东西吗?
If you use General Naming Conventions then the this keyword is redundant because the parameter should be id and the property should be Id based on Naming Guidelines. So it seems clear:
public int Id
{
get;
private set;
}
public VSOMessage(int id)
{
Id = id;
}
Run Code Online (Sandbox Code Playgroud)
Please note that the guidelines itself don't say, to use or not use this keyword but because C# is case sensitive, it would be a simplification to remove this keyword but when you don't use Naming Conventions then you may naming the property id instead ofId so you should use this keyword in such cases.
| 归档时间: |
|
| 查看次数: |
7511 次 |
| 最近记录: |