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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:17:44+00:00 2026-05-29T09:17:44+00:00

Is it possible to have a variable which is an instance variable within a

  • 0

Is it possible to have a variable which is an instance variable within a class but can only be accessed by a specific property?

I quite often create “self-creating” properties like so …

private IWGSLocation _location;
public IWGSLocation Location
{
    get
    {
        _location = _location ?? new WGSLocation();
        _location.Latitude = Latitude.GetValueOrDefault(0);
        _location.Longitude = Longitude.GetValueOrDefault(0);
        return _location;
    }
}

which means that I don’t go re-creating a new WGSLocation (or whatever other kind of object I need, which may be expensive to create, or may only need to be created once) every time I access the property. The downside is that my class can access the _location variable. But I don’t really want it to, so if there any way of having an instance variable which can only be used within the property itself?

I’m thinking something along these lines …

public IWGSLocation Location
{
    get
    {
        WGSLocation _location = _location ?? new WGSLocation();
        _location.Latitude = Latitude.GetValueOrDefault(0);
        _location.Longitude = Longitude.GetValueOrDefault(0);
        return _location;
    }
}
  • 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-29T09:17:45+00:00Added an answer on May 29, 2026 at 9:17 am

    I agree it would be a nice language feature to have persistent locals — that is, variables whose lifetimes are based on the lifetime of the instance but whose scopes (the region of program text in which it is legal to access the variable by name) are local. It would be nice to have “static” locals as well, as some languages do.

    Sadly, this is not a feature of C# and we have no plans to add it. It’s nice to have, but nice to have is not good enough to justify the expense, or to postpone or cancel a “nicer to have” feature.

    It’s only “nice to have” because of course if you have a private field, it already is a private implementation detail of the class. If you don’t want it used outside the property, then don’t write code that uses it outside the property. If one of your coworkers tries to do so, put the smack down on ’em in code review.

    I thought I might add: be very careful when writing property getters that mutate state. By default property getters are evaluated while looking at an object in the debugger, and it can be very confusing to be debugging something and have the debugger changing the values of fields just because you are examining an object.

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

Sidebar

Related Questions

Is it possible to have multiple versions of the same class which differ only
I have a class called Note , which includes an instance variable called time_spent
Is it possible in C# to have a Struct with a member variable which
Possible Duplicate: Should I initialize variable within constructor or outside constructor I have here
In a static class, I have a method which will edit a variable. The
I have a variable in a global scope which is assigned an instance of
Is it possible to have a variable number of fields using django forms? The
If I have an IEnumerator variable is it possible to have a lambda function
Is is possible to have a local variable in an anonymous c# methods, i.e.
Is it possible to access struts2 variable in jsp scriptlet? If I have struts2

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.