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

  • SEARCH
  • Home
  • 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 8059077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:33:25+00:00 2026-06-05T09:33:25+00:00

I have a public readonly property that calculates its value based upon a couple

  • 0

I have a public readonly property that calculates its value based upon a couple of other private variables. I read from the calculated property a lot. The variables that are used in the calculation change very rarely. I currently have the getter of the readonly perform the calculation logic, but it makes more sense to me to push the new value into the property when the dependant variables change.

An example of my current code:

public class rectangle()
{
    public rectangle(int height, int width)
    {
        _height = height;
        _width = width;
    }

    public void resize(int heightOffset, int widthOffset)
    {
        _height += heightOffset;
        _width += widthOffset;
    }

    private int _height;
    private int _width;
    public int area
    {
        get
        {
            return _height * _width;
        }
    }
}

The above code is overly simplified, but does a good job of showing what I mean. _height and _width can change, but they rarely ever do. The area property is read from a lot… I’d estimate that the _height/_width change once for every 100,000+ reads of the area property.

Now, if it was just multiplication, I wouldn’t be concerned… but the actual calcuations are complex and I’m seeing a lot of run time being sunk into repetitous superfluous calculations.

I know that I can do the following to fix it:

...

private int _height;
private int height
{
    set
    {
        _height = value;
        _area = _height * _width;
    }
}

private int _width;
private int width
{
    set
    {
        _width = value;
        _area = _height * _width;
    }
}

private int _area;
public int area
{
    get
    {
        return _area;
    }
}

...

The above code makes a lot more sense given my circumstances, but it just seems to me that there must be a better way to do this. I know that, given my first example, I could get rid of the properties and move the calculation logic to the resize() method… but in the real code it isn’t just one method that affects the private values. I don’t want to have to comb through my code to find every occurrence, and it doesn’t seem maintainable to do so.

Is the second method the way to go? I know it works, but something about it feels wrong. I think my concern is that it seems odd to have a private property storing it’s value into a private variable. I may be overthinking this.

  • 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. Editorial Team
    Editorial Team
    2026-06-05T09:33:27+00:00Added an answer on June 5, 2026 at 9:33 am

    Did you know property accessors can have access modifiers?

    For example:

    public string A
    {
        internal get
        {
            // Whatever
        }
        private set
        {
            // Whatever
        }
    }
    

    This works for auto-properties too:

    public string A
    {
        internal get;
        private set;
    }
    

    UPDATE:
    Ok, now you know about accessor access modifiers, what about this?

    public class rectangle()
    {
        public rectangle(int height, int width)
        {
            Height = height;
            Width = width;
        }
    
        public int Height { get; private set; }
        public int Width { get; private set; }
        public int Area { get; private set; }
    
        public void resize(int heightOffset, int widthOffset)
        {
            Height += heightOffset;
            Width += widthOffset;
    
            RefreshArea();
        }
    
        private void RefreshArea() 
        {
            Area = Height * Width;
        }
    }
    

    Goodbye, private fields!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following property definition: public static class Extensions { public static readonly
I have the following classes: Public Class Email Private Shared ReadOnly EMAIL_REGEX = \b[a-zA-Z]+[a-zA-Z0-9._+-]+@
I have the following in my model: public sealed class OAuthProvider { private readonly
I have public and private keys in separate .pem files that I would need
I have this one line property Public ReadOnly Property DateToUniversal(ByVal dt As Nullable(Of Date))
I have a custom Dependency Property defined like so: public static readonly DependencyProperty MyDependencyProperty
Public Class frmMain Private p_dlgAdd As frmAdd = Nothing Public ReadOnly Property _dlgAdd As
I have this class : public class TempFileRef { public readonly string FilePath; public
I have a Generic List as below public static readonly List<Customer> Customers = new
I have the following in a C# class: public static readonly SortedDictionary<string, string> Fields

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.