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

标签 c#

我是 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/

相关文章:

c# - 使用 MSI 安装版本设置 AssemblyInfo 版本号

c# - 可空的 Linq 表达式

C#/MEF 不适用于没有无参数构造函数的基类

c# - C# 中的 RtlCompressBuffer API

C# 如何在我的 Windows 窗体应用程序中的字符串内部进行一些基本数学运算

c# - 使用 Wordnik API 请求定义

c# - Linq 左连接将数据添加到左对象

c# - 从网页向数据库插入数据时,是否每次都使用 Trim 函数?

c# - 创建一个饲料墙

c# - unity 在最小和最大距离之间旋转