3

When initializing members in a C# class which also have Properties with full access (get, set), is it convention to initialize them inside the constructor via their Property method or directly by their member variable?

public class Car
{
    private string _brand;

    public Car(string brand)
    {
        // this
        _brand = brand;
        // or that
        Brand = brand;  
    }

    public Brand { get { return _brand; } set { _brand = value; } }
 }

7 Answers 7

4

You should set the value using the Property and not the member variable. This way you can change the implementation of your "setter" and only have to modify the your code in one place.

For example, if you discover a requirement that on setting the brand you have to also update some other property, the brand has a default colour for example, you can do this all in your set{...} block of the Brand property. If you set the value of _brand in your contructor, you'll now have to either manually update the Colour property in the constructor as well, or update your constructor to now initialise the Brand property instead of the field.

Also, where your property is that simple it's more conventional to use an "auto property"

public Brand { get; set; }

I'd only use a backing field if you need to perform more logic than a simple property set and get.

Sign up to request clarification or add additional context in comments.

1 Comment

I agree with a simple model such as this the private members just make the code less concise
2

Use the internal variable unless you have specific code in the Set method for the property that needs to fire.

7 Comments

There are other opinions on that. Mine is "Use always the property unless you have a performance problem (which will almost never be the case)". This way you are on the safe side should the implementation of the property ever change.
@codymanix - But isn't the point of the setter to set the internal variable? Therefore, calling the internal variable - regardless of the implementation of the setter - would return the same value anyway. Unless I'm misunderstanding what you mean?
@keyboardP the point is, the implementation of the set {...} might change (i.e. get more complex than just _field = value), if you're setting the backing field then your new logic within the setter doesn't get executed.
@BenCr - Oh, I see what you mean. Yup, that makes sense if your setter is more complicated.
Who's voting down without leaving a comment? Several answers indicate to use the member variable directly and were not down voted. You sir are detracting from the value of the site.
|
2

I always use the property accessors, because then your code is more prepared for changes down the road. For instance, as others have pointed out, the code you provided here can be rewritten using auto-properties. Additionally, if you use the property accessors throughout the class, you can then change the implementation of the property down the road and you'll only have to change code in one place.

1 Comment

Put it this way, if you use the accessors then you're always covered, but if you use the member variable directly then you're incurring some cost for later change.
1

When you construct a class it should be ready for use and requiring no further initialisation actions.

If your properties must be initialised in some way then this should be done via the constructor.

Comments

0

I've always done/seen the _brand = brand way. It makes sense, you are setting the internal state of the class. The properties are for external access.

Comments

0

I suggest their member variable, it's one less call on the stack.

1 Comment

Those are calls that'll almost certainly be optimised away by the jitter.
0

In the constructor, I prefer _brand = brand. I see getters and setters as methods to be called by external classes. There's also slightly less overhead as you're avoiding an extra method call.

1 Comment

The jitter will likely remove the extra call anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.