Resharper 5 中 C# 分配建议之前的冗余条件检查

发布于 2024-10-13 10:08:36 字数 678 浏览 2 评论 0原文

在下面的示例中,条件检查真的是多余的吗?:

public class MyClass     {
    public bool MyProperty { get; set; }

    public void DoSomething(bool newValue) {
        // R# says: redundant condition check before assignment
        // on the following line:
        if (MyProperty != newValue) { // <======
            MyProperty = newValue;
        }
    }
}

我知道无论哪种方式 MyProperty 都会被设置为 newValue,但是 check 是多余的吗?

在 Adob​​e Flex 中,只要 setter 运行,getter 就会由其运行的 VM 隐式调用即使没有进行显式检查也会被调用。最终结果是,在赋值之前进行检查会导致两次检查,一次显式检查,一次隐式检查,从而导致冗余检查。 C# 中是否也发生过类似的情况?

Is the condition check really redundant in the following sample?:

public class MyClass     {
    public bool MyProperty { get; set; }

    public void DoSomething(bool newValue) {
        // R# says: redundant condition check before assignment
        // on the following line:
        if (MyProperty != newValue) { // <======
            MyProperty = newValue;
        }
    }
}

I know that either way MyProperty will be set to newValue, but is the check redundant?

In Adobe Flex, the getter is called implicitly by the VM its running on whenever a setter is called even though no explicit check is being made. The end result is that checking before an assignment results in two checks, one explicit and one implicit, resulting in a redundant check. Does anything similar happen in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

メ斷腸人バ 2024-10-20 10:08:36

我只在两种情况下见过这种类型的支票。

第一种情况是,有一行额外的代码将对象的另一个属性设置为 True,以指示该对象已被修改。这通常在尝试决定是否将对象的状态保存到数据库之类的东西时使用。

第二种情况是所讨论的类型是不可变的。您可能希望避免设置该值并因此创建新字符串,例如,当值相同时。即便如此,我也只在某些内存使用至关重要的应用程序中看到过它。

There are only two situations where I've seen this type of check.

The first is when there is an additional line of code which sets another property on the object to True to indicate that the object has been modified. This is typically used when trying to decide whether to persist the state of the object to something like a database.

The second situation is when the types in question are immutable. You might want to avoid setting the value and therefore creating a new string, for example, when the values are the same. Even then, I've only seen it in certain apps where memory usage is critical.

窝囊感情。 2024-10-20 10:08:36

在这种特定情况下,它在逻辑上是多余的,因为 getter 中没有执行任何代码 - 只是私有字段的直接包装。如果您习惯在 getter 中放入会产生副作用的内容,我会建议禁用 R# 警告。

可能值得尝试在属性的 getter 中放入一些东西,看看 ReSharper 是否仍然认为它是多余的。如果确实如此,那么我将其称为 R# bug。

In this specific case, it's logically redundant, since there is no code being executed in the getter - just a straight wrapper around a private field. If you're in the habit of putting stuff in your getter that would have side effects, I'd say to disable that R# warning.

Might be worth trying to put something in the getter of the property, and see if ReSharper still thinks it's redundant. If it does, then I'd call that a R# bug.

新雨望断虹 2024-10-20 10:08:36

我想说支票是多余的。如果您有 INotifyPropertyChanged,但是检查将在 setter 中进行,以避免在没有进行实际更改的情况下触发事件。

I would say that the check is redundant. It would make more sense if you had an implementation of INotifyPropertyChanged, but then the check would be in the setter to avoid triggering the event if no actual change is done.

梨涡 2024-10-20 10:08:36

if (MyProperty != newValue) 是多余的,保留该行将产生相同的结果

if (MyProperty != newValue) IS redundant, leaving the line will yield the same result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文