简体   繁体   English

在Property Setter中,只有在价值不同时设置才有利吗?

[英]In a Property Setter is it Beneficial to only Set if Value Differs?

I'm wondering what benefits this code has: 我想知道这段代码有什么好处:

    private int _TestID;
    public int TestID
    {
        get 
        { 
            return _TestID;
        }
        set
        {
            if(_TestID != value)
            {
                _TestID = value;
            }
        }
    }

vs. this: 与此:

    private int _TestID;
    public int TestID
    {
        get 
        { 
            return _TestID;
        }
        set
        {
            _TestID = value;
        }
    }

It seems to me that this was done in the name of efficiency (only setting if different), but wouldn't the test take as long (or longer) that the initial set? 在我看来,这是以效率的名义完成的(只有设置如果不同),但测试不会花费与初始设置一样长(或更长)的时间吗? I'm not sure if I'm missing something here, but would love to hear any comments and explanations. 我不确定我是否在这里遗漏了一些东西,但我很乐意听到任何评论和解释。

It is useful when you combine it with a RaisePropertyChanged("TestID") (inside the if, after the field value is set) event pattern often seen with WPF or other databinding solutions. 将它与RaisePropertyChanged(“TestID”)(在if中,在设置字段值之后)组合在WPF或其他数据绑定解决方案中常见的事件模式时,它非常有用。

class Something : INotifyPropertyChanged
{
      public int TestID 
      {
           get { return testId; }
           set 
           {
                if (testId != value)
                {
                     testId = value;
                     RaisePropertyChangedEvent("TestID");
                }
           }
      }
 }

This is the type of optimization I will happily leave to the compiler. 这是我很乐意留给编译器的优化类型。 I would hate to "force" an efficiency that may or may not be true in every situation. 我不愿意“强迫”在每种情况下可能或可能不是真实的效率。

Really I would think that the first example would cause more problems when it comes to complex types and operator overloading. 真的,我认为第一个例子在涉及复杂类型和运算符重载时会引起更多问题。 In this particular case the first example is fairly pointless. 在这种特殊情况下,第一个例子毫无意义。

If performance is the only issue here, I would choose the first one.... Even IF there is a difference in performance, it will be too small to notice. 如果性能是这里唯一的问题,我会选择第一个....即使如果性能有所不同,它也会太小而无法注意到。

It's also a needless expansion of lines of code. 它也是不必要的代码行扩展。

I think the example code you gave is not fully correct. 我认为你提供的示例代码并不完全正确。 Did you mean this? 你的意思是?

private int _TestID;
public int TestID
{
    get
    {
        return _TestID;
    }
    set
    {
        if (_TestID != value)
        {
            _TestID = value;
        }
    }
}

The reason for this construction is not clear to me either. 这种结构的原因我也不清楚。 If however _TestID would be a property instead of an int then this construction could be beneficial because setting _TestID could in this case be an expensive operation. 但是,如果_TestID属性而不是int,那么这种结构可能是有益的,因为在这种情况下设置_TestID可能是一项昂贵的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM