C#只读属性来计算,他们应该是方法? [英] C# read-only calculated properties, should they be methods?

查看:180
本文介绍了C#只读属性来计算,他们应该是方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经计算出对他们的领域,如TOTALCOST几个实体。现在,我把它们都作为属性,但我不知道他们是否实际上应该是方法。是否有此一C#标准



 公共类工作单
{
公众诠释LaborHours {得到什么?;组; }
公共小数LaborRate {搞定;组; }

//如果这是LaborCost()?
公共小数LaborCost
{
得到
{
返回LaborHours * LaborRate;
}
}
}


解决方案

这是确定使用计算的特性,而不是方法,只要计算没有明显的时间



请参阅Property使用指南


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?

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

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

解决方案

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

这篇关于C#只读属性来计算,他们应该是方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆