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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:18:44+00:00 2026-05-11T19:18:44+00:00

In the following setter, I can access the property backing field directly or through

  • 0

In the following setter, I can access the property backing field directly or through the getter. Is there a scenario when one would be preferred over the other?

public string Name {
        get { return this.name; }
        set {
            if (value == this.name) return;
            // or
            // if (value == this.Name) return;
            // ?
            this.name = value;
            NameChanged.Raise(this, this.name);
            // or
            // NameChanged.Raise(this, this.Name);
            // ?
        }
    }

There is a related question. How would you initialize properties in the c-tor?

public MyClass(string name) { this.name = name; }
// or
public MyClass(string name) { Name = name; }

I use this.name, for the reason that at construction time the instance might be in an invalid/unstable/undefined state, so Name-setter validation might falsely fail. Any other opinions?

  • 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-11T19:18:44+00:00Added an answer on May 11, 2026 at 7:18 pm

    My personal opinion is to preferably use the property unless that results in the incorrect behaviour. What it comes down to is that using the property indicates a commitment to the semantics of your class and the design of your API.

    Obviously sometimes there are going to be exceptions to this rule… sometimes the ‘property’ means something distinct to the value of the backing field (in your example, the property raises an event). If the internal use explicitly needs to avoid the semantics of the property (you don’t want the event to fire), then the backing field is the correct ‘second choice’.

    On an unrelated note, for better or for worse, the Microsoft StyleCop application specifically prefers the convention of accessing private fields with the ‘this’ prefix to differentiate access of local variables and class fields (rather than prefixing such as ‘_‘ or ‘m_‘ or variants thereof… which ironically are the convention used in legacy .NET framework code).

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

Sidebar

Related Questions

How can I create a public getter and a private setter for a property?
Lets suppose i have a field with short defined getter setter like following: public
Can I freely add additional operations in C# public property's getter and setter? To
How can i use a custom setter for the following property after I synthesized
I have the following scenario : Class A{ private List<Long> longList; //getter and setter
I have following user control: <UserControl.Resources> <Style TargetType=HeaderedItemsControl> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType=HeaderedItemsControl> <Grid>
I am trying to achieve the following: <Style TargetType=ListBoxItem> <Setter Property=ContextMenu> <Setter.Value> <ContextMenu> <MenuItem
I have the following classes... class ExpressionBinder<T> { public Func<T> Getter; public Action<T> Setter;
Imagine the following template class (setter and getter for the member _t omitted): template<class
I wrote a setter and getter method following Apple's conventions and noticed that despite

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.