简体   繁体   English

在属性getter中或在创建实例时进行评估?

[英]Evaluate in property getter or when creating an instance?

Ok, an easy question. 好的,一个简单的问题。

First of all, I have to say that my concern is not performance. 首先,我不得不说我关心的不是表现。 I'm totally aware that whatever performance costs one option or the other may entail are probably meaningless and not even worth considering in normal scenarios. 我完全清楚,无论一个选项或另一个选项可能带来什么样的性能成本都可能毫无意义,甚至在正常情况下都不值得考虑。 It has more to do with design standards and curiosity as to how the majority of coders would do it. 它更多地与设计标准和好奇心有关,大多数编码人员将如何做到这一点。

Ok, so the question is rather simple: 好的,所以问题很简单:

Suppose I have a ComplexNumber struct which I could implement the following way: 假设我有一个ComplexNumber struct ,我可以通过以下方式实现:

public struct Complex : IEquatable<Complex>, IFormattable
{
    readonly double realPart, imaginaryPart, magnitude, argument;
    readonly static Complex j = new Complex(0, 1);

    public Complex(double realPart, double imaginaryPart)
    {
        this.realPart = realPart;
        this.imaginaryPart = imaginaryPart;
        this.magnitude = Math.Sqrt(Math.Pow(realPart, 2) + Math.Pow(imaginaryPart, 2));
        this.argument = Math.Atan2(imaginaryPart, realPart);
    }

    public double RealPart { get { return this.realPart; } }
    public double ImaginaryPart { get { return this.imaginaryPart; } }
    public double Magnitude { get { return this.magnitude; } }
    public double Argument { get { return this.argument; } }

    public static Complex J { get { return Complex.j; } }
    ...
 }

The Magnitude and Argument properties have backing fields that are evaluated at construction time. Magnitude和“ Argument属性具有在构造时评估的后备字段。 Another option would be to simply evaluate the corresponding value in either getter. 另一个选择是简单地评估任何一个getter中的相应值。

What is the most recommended way to do this? 最推荐的方法是什么? Is there any coding standard that recommends any option for the sake of having a standard? 是否有任何编码标准为了有标准而推荐任何选项? And if there isn't one, what is normally the preferred choice? 如果没有,通常首选的是什么? Or is it only performance dependant which in my case is irrelevant? 或者它只是性能依赖,在我的情况下是无关紧要的?

[UPDATED BELOW:] [更新如下:]

Why evaluate in the getter and not in the setter? 为什么要在吸气剂中而不是在定位器中进行评估? I would evaluate the value as it is being set. 我会评估它的设置值。 That way the correct value can be used in private methods. 这样,正确的值可以在私有方法中使用。

Set defaults in c'tor, evaluate in setter. 在c'tor中设置默认值,在setter中进行评估。

You will always read the value more often than setting it, so for performance reasons you should do the evaluation in the setter - it will be run less often. 您将始终比设置值更频繁地读取值,因此出于性能原因,您应该在setter中进行评估 - 它将更少运行。

[UPDATE:] If the property is read-only then evaluate in the c'tor, for the same reasoning as above (performance - you will only do the evaluation once). [更新:]如果属性是只读的,则在c'tor中进行评估,其结果与上述相同(性能 - 您只需进行一次评估)。 I know you say performance is not an issue, but if there are no reasons not to do it in the better performing way then it should be done like that. 我知道你说性能不是问题,但如果没有理由不以更好的方式做到这一点,那么应该这样做。

I would favor computing the values directly in getters, because it's more readable: If you want to know what Argument does, just look at its code. 我倾向于直接在getter中计算值,因为它更具可读性:如果你想知道Argument作用,只需看看它的代码。 If you cached the value in a field like you do now, you have to go Argument property → argument field → constructor. 如果您像现在一样在字段中缓存值,则必须转到Argument属性→ argument字段→构造函数。

If performance did matter, obviously the proper way to find out which option is better in your case is profiling. 如果性能确实很重要,那么在您的情况下找出哪个选项更好的正确方法就是分析。 But as a guess , I think the version with values cached in fields will be slower too, especially if you don't use the computed values often. 但是作为猜测 ,我认为在字段中缓存值的版本也会变慢,特别是如果您不经常使用计算值。 That's because struct s are copied all the time and those fields make the struct twice as big. 那是因为struct被复制,而那些字段使struct变得两倍大。

C'tor should initialize the members with default values. C'tor应该使用默认值初始化成员。

Definitely, performance can become issue if you move the code to getter because you'll evaluate each time getter is called. 当然,如果将代码移动到getter,性能可能会成为问题,因为每次调用getter时都会评估。

However, The contract of getter says that it'll get the value. 然而,吸气者的合同说它会获得价值。 so calculation part should be avoided as much as possible in getter block. 所以在吸气剂阻滞中应尽可能避免计算部分。

And you should also try to avoid the use of dirty values. 你还应该尽量避免使用脏值。 if you set the Magnitude (say) in getter but try to access it within class directly; 如果你在getter中设置了Magnitude (比如说),但试着直接在课堂上访问它; you might end-up in using wrong value. 你最终可能会使用错误的价值。

Hence, if the question is about initilizing member variables, do it in c'tor. 因此,如果问题是关于初始化成员变量,请在c'tor中进行。 that's why it is created by language designers. 这就是它由语言设计师创造的原因。

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

相关问题 什么时候可以在属性getter访问权限上更改对象状态(例如初始化)? - When is it ok to change object state (for instance initialization) on property getter access? 创建实例时如何确定属性的类型? - How to decide for type of a property when creating an instance? 创建接口实例时将属性传递给类 - Pass property into a class when creating an instance of an interface 为属性setter或getter创建一个高性能的开放委托 - Creating an performant open delegate for an property setter or getter 何时使用GetXXX()方法以及何时具有Getter属性 - When to use GetXXX() method and when a Getter property 使用char []属性创建对象的实例 - Creating an instance of an object with a char[] property 特定属性更改时评估多重绑定 - Evaluate Multibinding when a specific property changes 创建挂钩到属性的getter和setter的C#属性 - Creating C# attribute that hooks into property's getter and setter 在公共实例上使用getter和setter设置该类的私有实例时,会设置哪个类实例的属性? - Which class instance's properties are set when using a getter and setter on a public instance to set a private instance of that class? 创建一个属性来存储实例C# - creating a property to store an instance c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM