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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:20:15+00:00 2026-05-22T19:20:15+00:00

Possible Duplicate: c#: why have empty get set properties instead of using a public

  • 0

Possible Duplicate:
c#: why have empty get set properties instead of using a public member variable?

string name;

vs

string name {get; set;}

Assuming your get and set are blank as above, what’s the point in specifying them?

  • 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-22T19:20:16+00:00Added an answer on May 22, 2026 at 7:20 pm

    It encapsulates the compiler generated field, and provides you, the class or struct developer the ability to update it internally later without breaking your API by simply modifying the get/set part that you care about.

    For instance, suddenly never want to return null? You can do that by simply changing the empty get to get { return storedName ?? ""; }. Of course, it means you suddenly need to manually control the variable, but that’s a small price to pay for the flexibility.


    The first use is an example of a field declaration. The second use is an example of an auto-implemented property.

    It is generally bad practice to provide direct access to a field. However, the .NET team noticed that a lot of getters/setters are basically just that. For example, consider the following:

    // C#
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    
    // Without properties (or a Java implementation)
    public void setName(String name)
    {
        this.name = name;
    }
    
    public String getName()
    {
        return name;
    }
    

    Either way, that’s a lot verbosity to really just expose a field. However, it is regularly the case that, as a developer, you need to go back and change how a field is handled internally, but you do not want to break or even affect other code if you can get away with it.

    That is why using direct access to fields is bad. If you provide direct access to fields, but need to change something about using the field, then all code that uses that field must change as well. If you use a property (or even a method), then you can change the internal code and potentially not effect external code.

    Consider the following example:

    public string Name
    {
        get;
        set;
    }
    

    Later you decide that you need to raise a changing and changed event around the setter. If you exposed a field, then it’s time for a potentially big rewrite. If you used properties (or a method), then you can just add the logic there. You suddenly lose the benefit of auto-implementing properties, but you gained the ability to refactor your class without breaking existing code.

    private string name;
    public event NameChangingEventHandler NameChanging;
    public event NameChangedEventHandler NameChanged;
    
    public string Name
    {
        get { return name; }
        set
        {
            OnNameChanging(/*...*/);
            name = value;
            OnNameChanged(/*...*/);
        }
    }
    
    protected virtual void OnNameChanging(/*...*/) { }
    protected virtual void OnNameChanged(/*...*/) { }
    

    All of that maintains your public API and requires no work from users of the class (the rest of your code, or external developers using of your API). Breaking changes are not always avoidable, but avoiding direct access to fields is a good step to try to ensure that it won’t happen. Auto-implemented properties are a quick, and easy way to do it.

    (Unrelated: lost power while typing this and I am very happy that my browser saved most of it!)

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

Sidebar

Related Questions

Possible Duplicate: Comparing object properties in c# Let's say I have a POCO: public
Possible Duplicate: Split string in SQL I have seen a couple of questions related
Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: C#: Public Fields versus Automatic Properties Duplicate? I think not: This question
Possible Duplicate: JSON serialization of c# enum as string I have two classes as
Possible Duplicate: Paypal Checkout Express empty cart problem i have an issue with paypal
Possible Duplicate: Conditional Linq Queries Using Entity Framework 4.0 I have a search condition
Possible Duplicate: splitting a string i have a string which looks like this: http://pastebin.com/m5508ff19
Possible Duplicate: IE has empty document.referrer after a location.replace Hi there, I have got
Possible Duplicate: Get just the hour of day from DateTime using either 12 or

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.