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 561707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:28:40+00:00 2026-05-13T12:28:40+00:00

I was wondering, what is good practice: private int value; public int Value {

  • 0

I was wondering, what is good practice:

 private int value;

 public int Value { get { return this.value; } }

 private int DoSomething()
 {
      return this.Value + 1;
      //OR
      return this.value + 1;
 }

So, the question is about how you should treat your class variables. Should you access them through your properties or just direct?

  • 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-05-13T12:28:40+00:00Added an answer on May 13, 2026 at 12:28 pm

    In this case, it doesn’t matter so much, but in cases where you’re using lazy loaded variables it would matter and you’d access through your property. For instance:

    private Myobject _value;
    
    public Myobject Value
    {
        get
        {
            if (_value == null) _value = new MyObject();
            return _value;
        }
    }
    
    private MyObject DoSomething();
    {
        //If you access _value here, it might be null...
        //so you should access it through the property:
        return Value;
    }
    

    In a case where your method would fail by calling the field directly, you either handle it properly, or you access it by the cleaner method – via your property.

    It all comes down to the architecture of your application and for that you have to ask the question: What makes the most sense from a maintenance perspective?

    I would have to say that if the field is initialized properly and it doesn’t cause you a headache to access it directly, then access it directly. If it causes extra work (and thus extra maintenance), then reference the property.

    Like with anything else, weigh up the pros and cons. Use your own common sense – standards wars are a ridiculous distraction. Unless they provide you with arguments you haven’t already thought of yourself, don’t waste your breath fighting with them. If you have a valid reason to choose whichever path you choose, then that path is the right one – it comes down to the designer’s prerogative.

    My thoughts on pros and cons are this:

    Going with property:

    • I can change the implementation of how that value is returned without needing to refactor my whole class.
    • I can lazy load the value which means that if I never access it, it never wastes resources.
    • I can hide all the implementation details of how the value is returned in a single place without having to handle it all over my code.

    Going with the field:

    • I don’t have the potential performance overhead of having to step through the property code each time I access the value.
    • I need to make sure the value is initialized properly on every call, or handle cases where it is not.
    • I can affect the value even though my property may only provide a read-only interface to my value.

    So I guess my approach would be to use the property unless I needed to write to the value directly, in which case, I’d go with the field – as my property is read-only and thus can’t be written to.

    That’s just me though – your property may be read/write and you may decide from a design standpoint that accessing the field directly is okay – and that is okay too.

    The trick is always do things for a reason, don’t do things blindly just because.

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

Sidebar

Related Questions

I'm wondering if there are any good guides or books that explain the best
I will use a simple example to illustrate my question. In Java, C, or
I'm from the world of C# originally, and I'm learning C++. I've been wondering
I'm relatively new to using oop in Javascript, and I'm wondering what the best
I am woundering whether someone has time to normalize my database? I dont know
So like most new .NET developers you start of passing DataSets everywhere and although
I'm building an Active Directory wrapper in VBNET 2.0 (can't use later .NET) in
I am using the CountDownLatch to synchronize an initialization process between two threads and
Image a Button on your windows form that does something when being clicked. The

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.