Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 117983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:25:21+00:00 2026-05-11T03:25:21+00:00

which is better ??? public class Order { private double _price; private double _quantity;

  • 0

which is better ???

public class Order {    private double _price;    private double _quantity;    public double TotalCash   {          get    {     return _price * _quantity;    } } 

or

public class Order {     private double _totalCash;    private double _price;    private double _quantity;    private void CalcCashTotal()  {    _totalCash = _price * _quantity  }    public double Price   {          set    {      _price = value;      CalcCashTotal();    }   }    public double Quantity   {          set    {      _price = value;      CalcCashTotal();    }   }     public double TotalCash   {          get    {     return _totalCash;    } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:25:22+00:00Added an answer on May 11, 2026 at 3:25 am

    There are tradeoffs. If the calculations simple and does not take a long time, then put it in the get. It makes your life easier, because you don’t have to worry about doing the check in every set that total price is dependant on, which could lead to mistakes.

    If the calculation takes a lot of time, then you could also take a hybrid approach. You could set a IsDirtyTotalPrice boolean in all the dependant sets, and then do the calculation on the fly in the get and cache it so that the get only calculates the variable when it needs to. You don’t do the calculation in the sets because there could be a lot of them, and you wan to do the calculation as few times as possible.

      public class Order   {      private double _totalCash;      private double _price;      private double _quantity;      private _IsDirtyTotalCash = true;    private void CalcCashTotal()   {     _totalCash = _price * _quantity   }    public double Price   {          set    {      _price = value;      _IsDirtyTotalCash = true;    }   }    public double Quantity   {          set    {      _price = value;      _IsDirtyTotalCash = true;    }   }    public double TotalCash   {          get    {         if(_IsDirtyTotalCash)     {       _totalCash = CalcTotalCost();        _isDirtyTotalCash = false;      }      return _totalCash;    }   }  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.