title: Optimizing by caching results of calculations to string variables
body: <p>I have several classes which will have calculated members exposed to the UI. I'm trying to optimize such that the readout only gets a fresh value when needed.</p>
  
  <p>For instance, I have a Container which can hold Items. Items have UnitMass, UnitSize, UnitCount, etc. Items also calculate their TotalMass (UnitMass x UnitCount), TotalSize, etc. Containers calculate their TotalContentMass, TotalContentSize, and so on by summing the respective members of their contents.</p>
  
  <p>A Container owner might have visible readouts of TotalContentMass, etc. for a hundred containers at any time. Any number any container reads might be changing 30x per second, or might remain the same for hours on end.</p>
  
  <pre>
  [Container 1] TotalContentMass: 218 | AvailableMass: 1991
       [Item 1] Units: 5  | TotalMass: 50
       [Item 2] Units: 3  | TotalMass: 96
       ...
  [Container 2] TotalContentMass: 944 | AvailableMass: 53
       [Item 1] Units: 12 | TotalMass: 16
       [Item 2] Units: 21 | TotalMass: 488
       ...
  ...
  </pre>
  
  <p>So for these kinds of "TotalX" members, I thought I'd have a field which stored the ToString result of the calculation, and use that string for most UI purposes. I'd rely on event handling to keep the string updated.</p>
  
  <pre><code>class Item
  {
      int _unitCount;
      int _unitMass;
      string _totalMassText;
      int _unitSize;
      string _totalSizeText;
  
      ...
  
      public TotalMass { get { return _unitMass * _unitCount; } }
      public TotalMassText { get { return _totalMassText; } }
  
      public TotalSize { get { return _unitSize * _unitCount; } }
      public TotalSizeText { get { return _totalSizeText; } }
  
      public UnitCount 
      { 
          get { return _unitCount; } 
          set { 
                  _unitCount = value; 
                  OnUnitCountChanged();
              } 
      }
  
      ...
  
      UpdateTotals()
      {
          _totalMassText = TotalMass.ToString();
          _totalSizeText = TotalSize.ToString();
          ...
      }
  }
  </code></pre>
  
  <p>And the same for Container, and so on.</p>
  
  <p>Is this premature or micro-optimization? Does the compiler take care of this sort of thing in the first place? If not, is this a fairly standard approach?</p>