我是一个相对较新的C#,在理解这条消息时遇到了一些麻烦,以及它是如何导致问题的.在安装resharper之前没有显示错误,所以我认为它只是糖语法错误?
public void SetTransform(float x, float y, float angle)
{
SetTransform(x, y);
this.angle = angle;
}
Run Code Online (Sandbox Code Playgroud)
这个领域本身:
float angle;
Run Code Online (Sandbox Code Playgroud)
我很困惑,方法中的参数如何隐藏字段变量?...
这是一个警告,告诉您可能会混淆这两个变量:
class IDontKnow
{
float angle;
public void SetTransform(float x, float y, float angle) {
SetTransform(x, y);
this.angle = angle; // Its not really clear by the naked eye which angle is used.
}
}
Run Code Online (Sandbox Code Playgroud)
我建议使用下划线重命名字段角度,如下所示:
class IDontKnow
{
float _angle;
public void SetTransform(float x, float y, float angle) {
SetTransform(x, y);
_angle = angle; // using underscore as a prefix makes the use of this-keyword redundant.
}
}
Run Code Online (Sandbox Code Playgroud)
通常,您需要一些"清晰"的命名约定,并对(受保护的和更高范围的)字段和属性,方法参数和局部变量进行清晰的区分.这使代码更具可读性并避免上述警告.