简体   繁体   English

不变值和自动实现的属性

[英]Non-changed value and auto-implemented properties

When implementing simple properties, you can make use of auto-implemented properties , to make the code look simpler, like this: 实现简单属性时,可以使用自动实现的属性 ,使代码看起来更简单,如下所示:

public int MyInt
{
    get; set;
}

Or you can implement it with a "manual" private field. 或者,您可以使用“手动”专用字段来实现它。 When this is the case, I have often seen people checking for value changes before using set . 在这种情况下,我经常看到人们在使用set之前先检查值的更改。 Like this: 像这样:

private int _myInt;

public int MyInt
{
    get { return _myInt; }
    set 
    {
        if value != _myInt
            _myInt = value;
    }
}

So my question is this: Is checking for the changed value (in the latter case) good for anything? 所以我的问题是:检查更改后的值(在后一种情况下)是否对任何事情都有利? And if it is, then what impact does this have on the auto-implemented properties? 如果是的话,这会对自动实现的属性产生什么影响?

You would use that check when you implement INotifyPropertyChanged 在实现INotifyPropertyChanged时将使用该检查
(You want to fire an event only when the value of the property actually changes, not simply when the setter is called.) (您只想在属性值实际更改时才触发事件,而不仅仅是在调用setter时才触发。)

You can´t do both, use an auto-implemented property and check its value. 您不能同时使用自动实现的属性和检查其值。 As soon as you introduce any logic into either the getter or the setter, it´s not any more an auto-implemented property. 一旦将任何逻辑引入getter或setter中,它就不再是自动实现的属性。

The auti-implementing property is just a syntactic sugar for a getter/setter accessing a private backing-field as if you´d write this: auti-implementing属性只是获取/设置者访问私有后备字段的语法糖,就好像您要编写以下代码:

public int MyInt
{
    get { return _myInt; }
    set { _myInt = value; }
}

When you want to check if the new value even is valid you have to use a setter with some logic within. 当您要检查新值是否有效时, 必须使用带有一些逻辑的设置器。 For example you may want to check if your integer is positive because your application can´t handle negative values. 例如,您可能要检查整数是否为正,因为应用程序无法处理负值。 So you simply introduce that check in the setter. 因此,您只需在设置器中介绍该支票即可。

set 
{
    if (value >= 0)
        _myInt = value;
}

As an aside you can´t define just one part (that is either the getter or the setter), you have to define both with braces. 顺便说一句,您不能只定义一个部分(即吸气剂吸气剂),而必须用花括号来定义两者。 On the other side you can of course completely omit one of them, making your property readonly or write-only (although I can barely think of any use of the latter). 另一方面,您当然可以完全忽略其中之一,使您的属性为只读或仅写(尽管我几乎无法想到后者的任何用法)。 Actually even properties are syntactic sugar for a get- and a set-method. 实际上,即使属性是获取方法和设定方法的语法糖。 So you can think of them as usual methods. 因此,您可以将它们视为常用方法。 Why shouldn´t you introduce any logic there? 为什么不在那里引入任何逻辑? Of course properties tend to make people think that nothing special is happening within their setter or getter, and this is also what conventions suggest. 当然,属性倾向于使人们认为他们的二传手或getter中没有发生什么特别的事情,这也是惯例所暗示的。 However there´s actually nothing special on them except the fact that on auto-implementing properties your just accessing a backing-field which you can´t access in your code as it´s name isn´t known to the IDE, however it exist within the IL as shown here . 但是,它们实际上没有什么特别之处,除了以下事实之外:IDE仅知道在自动实现属性上仅访问您无法在代码中访问的后备字段的名称,因为它的名称在IL内, 如此处所示

EDIT: To come back to your question. 编辑:回到您的问题。 It won´t make a real difference if you´d introduce the logic for validation of the new value within the setter or before it, thus you can also write this: 如果您要在设置器内或之前引入用于验证新值的逻辑,那将不会有什么真正的区别,因此,您也可以这样编写:

int newValue = -1;
if(newValue >= 0)
    myInstance.MyInt = newValue;

But this could be quite messy and in particular breaks the DRY -priniciple as soon as you´d need to set the property somewhere else in your code and check it again. 但这可能会很混乱,尤其是当您需要在代码中的其他位置设置属性并再次检查时,就会破坏DRY -priniciple。 So it´s better to do it once, and only once (within the setter). 因此,最好一次执行一次 (在设置员内部)。

Is checking for the changed value (in the ladder case) good for anything? 检查更改的值(在梯形图情况下)是否对任何事情都有利?

Not really, at least not in the example you provided. 并非如此,至少在您提供的示例中没有。 Well, unless you have code that assigns to the property in a huge loop (hundred thousands), in which case, if checking the value is milliseconds faster than the assignment, then you will see a noticeable difference. 好吧,除非您有一个在巨大的循环(十万个循环)中分配给该属性的代码,在这种情况下,如果检查值比分配的速度快毫秒,那么您将看到明显的不同。 I said "if", because I'm not sure it is the case for int , so you'll have to test it if you're interested in the answer. 我说“如果”,是因为我不确定int是否适用,因此如果您对答案感兴趣,就必须对其进行测试。

However, in other scenarios, there could be a noticeable difference. 但是,在其他情况下,可能会有明显的差异。 For example, if the setter is more than just assigning the value. 例如,如果setter不仅仅是分配值。

Lets assume MyInt is X location of your mouse, you have a infinite loop that takes mouse location every milisecond, and shows on a window. 让我们假设MyInt是鼠标的X位置,您有一个无限循环,它每隔一毫秒就获取一次鼠标位置,并显示在窗口上。 If you do not check you have to update window every miliseconds, when your mouse is standing still. 如果不进行选择,则当鼠标静止不动时,必须每隔毫秒更新一次窗口。

Most of the cases you do not need to check, even if it is the same value , value is changed. 大多数情况下,即使它是相同的value ,也不需要检查value ,但值已更改。 Do it when you have performance issues. 当您遇到性能问题时,请执行此操作。

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

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