gpt4 book ai didi

c# - 参数 'angle' 隐藏字段 'float Utils.Transform.float'

转载 作者:太空宇宙 更新时间:2023-11-03 17:50:50 25 4
gpt4 key购买 nike

我是 C# 的新手,在理解这条消息以及它是如何导致问题时遇到了一些困难。在我安装 resharper 之前错误没有出现,所以我认为这只是 sugar 语法错误?

public void SetTransform(float x, float y, float angle)
{
SetTransform(x, y);
this.angle = angle;
}

以及字段本身:

float angle;

我很疑惑,方法中的参数怎么会隐藏一个字段变量?...

最佳答案

这是一个警告,告诉您您可能会混淆这两个变量:

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.
}
}

我建议使用下划线重命名视场角,如下所示:

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.
}
}

一般来说,您需要一些“清晰”的命名约定,明确区分( protected 和更高范围的)字段和属性、方法参数和局部变量。这使代码更具可读性并避免了上述警告。

关于c# - 参数 'angle' 隐藏字段 'float Utils.Transform.float',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26938029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com