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

Jim Mitchener picture Jim Mitchener · Jan 8, 2010 · Viewed 34.7k times · Source

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;
        }
    }
}

Answer

Thomas Levesque picture Thomas Levesque · Jan 8, 2010

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