C# 只读计算属性,它们应该是方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2030636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:50:38  来源:igfitidea点击:

C# read-only calculated properties, should they be methods?

c#standards

提问by Jim Mitchener

I have several entities that have calculated fields on them such as TotalCost. Right now I have them all as properties but I'm wondering if they should actually be methods. Is there a C# standard for this?

我有几个实体计算了它们的字段,例如 TotalCost。现在我把它们都作为属性,但我想知道它们是否真的应该是方法。是否有 C# 标准?

public class WorkOrder
{
    public int LaborHours { get; set; }
    public decimal LaborRate { get; set; }

    // Should this be LaborCost()?
    public decimal LaborCost
    {
        get
        {
            return LaborHours * LaborRate;
        }
    }
}

采纳答案by Thomas Levesque

It's OK to use calculated properties rather than methods, as long as the calculation doesn't take a noticeable time

使用计算属性而不是方法是可以的,只要计算不花费明显的时间

See Property usage guidelines

请参阅属性使用指南

回答by Brian Mains

In my opinion, it's a preference; it's what you want to do. I do propreties in most cases, unless there is logic involved. Additionally, if you need to pass in parameters to change the functionality then obviously a method would apply...

在我看来,这是一种偏好;这就是你想要做的。在大多数情况下,我都会做属性,除非涉及逻辑。此外,如果您需要传入参数来更改功能,那么显然可以应用一种方法......

回答by Billy ONeal

I would leave them as properties. But there's not "standard" reason to do things one way or another. If you're by yourself, do whatever you like best. If you're on a team, then follow conventions the rest of your team are following.

我会把它们作为财产。但是没有“标准”的理由以一种或另一种方式做事。如果你一个人,做你最喜欢的事。如果您在一个团队中,则遵循团队其他成员所遵循的惯例。

回答by Cellfish

I think methods should perform actions on the object, typically change the state of the object. Properties should reflect the current state of the object even if the property is calculated. So you should keep your properties IMO.

我认为方法应该对对象执行操作,通常会更改对象的状态。即使计算了属性,属性也应该反映对象的当前状态。所以你应该保留你的财产 IMO。

回答by JonH

Depends, if your "properties" become mammoths and require a whole slew of business logic they shouldn't be properties, there should be a method. The example you posted looks ok to be a property. No standard way of doing it, go with your gut instinct; if it looks like it needs to do a lot you probably need a method.

视情况而定,如果您的“属性”变得庞大并需要大量业务逻辑,它们不应该是属性,那么应该有一种方法。您发布的示例看起来可以作为一个属性。没有标准的做法,跟着你的直觉走;如果它看起来需要做很多事情,你可能需要一种方法。

回答by Michael Stum

If they are a) lightweight and b) have no side effects, I would make them Properties.

如果它们 a) 轻量级且 b) 没有副作用,我会将它们设为属性。

Lightweight is a bit fuzzy of course, but the rule of thumb is: If I ever have to worry calling a Property (be it in a loop or anywhere else), it should possibly be a method.

轻量级当然有点模糊,但经验法则是:如果我不得不担心调用属性(无论是在循环中还是在其他任何地方),它应该是一个方法。

回答by Tim Ridgely

I think they should all be properties. As long as it doesn't change the state of the object, I'm cool with it as a property.

我认为它们都应该是属性。只要它不改变对象的状态,我就把它作为一个属性很酷。

Additionally, if I'm using your class for data binding (WPF, etc.), then I can bind directly to your property without having to modify/extend the class.

此外,如果我使用您的类进行数据绑定(WPF 等),那么我可以直接绑定到您的属性,而无需修改/扩展该类。

回答by Joel Mueller

If a property is particularly expensive to calculate, I might change it to a GetWhatever() method. This serves as a hint to whoever uses my class that this value requires some significant work to arrive at, and the caller should cache the value rather than calling the method multiple times.

如果某个属性的计算成本特别高,我可能会将其更改为 GetWhatever() 方法。这向使用我的类的任何人提示,该值需要一些重要的工作才能到达,并且调用者应该缓存该值而不是多次调用该方法。

Trivial calculations are perfectly appropriate inside of properties.

琐碎的计算非常适合在属性内部。

回答by Paul Creasey

It's largely just syntactic sugar anyway, so do want you is convention in your team, or what you prefer, as long as it is just returning information about the object and not changing it or interacting with other objects.

无论如何,它在很大程度上只是语法糖,所以确实希望你是团队中的约定,或者你喜欢什么,只要它只是返回关于对象的信息,而不是改变它或与其他对象交互。

回答by JCasso

MSDN gives information about this here

MSDN在此处提供了有关此的信息

Class library designers often must decide between implementing a class member as a property or a method. In general, methods represent actions and properties represent data.

类库设计者通常必须在将类成员实现为属性或方法之间做出决定。一般来说,方法代表动作,属性代表数据。

Which one do you think it is? An action calculate/getLaborCost or data?

你认为是哪一种?一个动作计算/getLaborCost 还是数据?

WorkOrder workOrder = new WorkOrder();
workOrder.LaborHours = 8;
workOrder.LaborRate = 20;

decimal cost = workOrder.LaborCost; // This is OK here

but if you are going to do this for the same object also:

但是如果您也打算为同一个对象执行此操作:

worOrder.LaborHours = 18;
decimal newCost = workOrder.LaborCost 

Now this cannot be a property. It would be a lot better to be a method.

现在这不能是财产。成为一种方法会好得多。