logan
logan

Reputation: 111

With C# Why would you use an accessor instead of just setting the variable equal to the property?

For example

Public int Width
{
    get { return Something.Width; }
}

instead of

Public int Width;
//later in the code
Width = Something.Width;

or

Public int Width = Something.Width;

Upvotes: 0

Views: 374

Answers (2)

William
William

Reputation: 772

Accessors are a very powerful feature, that allows you to attach methods and advanced visibility modifiers to your properties.

Fake Read-Only Example

public class Entity
{
    // This Health variable looks like a read-only variable from the outside, but is still settable outside the constructor.
    public Single Health { get; private set; }

    // This Resistance variable looks like a read-only variable from the outside, but is still settable outside the constructor.
    public Single Resistance { get; private set; }

    public void Damage(Single amount)
    {
        this.Health -= Math.Max(amount - this.Resistance, 0.00f);
    }
}

Method Example

public class Entity
{
    private World world;

    public World World
    {
        get { return this.world; }
        set
        {
            // This will ensure the entity is always added and removed correctly from the world it is set to belong to.
            if(this.world != null)
                this.world.RemoveEntity(this);

            this.world = value;

            if(this.world != null)
                this.world.AddEntity(this);
        }
    }
}

Advanced Visibility Example

public class Entity
{
    // This gives you a read-only style property, which can still be set by other classes inheriting this class, as the setter is protected.
    public Vector2 Position { get; protected set; }
}

I'm sure there are plenty of other examples, but this is some of the reasons accessors are a wonderful tool.

Note that an accessor always gets the default value, and you can only change this in the constructor.

Default values

  • byte, short, int, long, float, double: Zero
  • string: An empty string.
  • classes: null
  • structs: The default value for their members types.

Upvotes: 6

Patrick Hughes
Patrick Hughes

Reputation: 357

1) Width is just a question you can ask about an object, how wide are you? Outside you don't really care how width is dealt with inside, you just care about what is the answer.

2) Width may change, right now it's just a width member variable but later maybe it's a calculation or maybe the object is really a list of other objects. The object itself should be responsible for all that and not someone outside who is not-the-object.

3) The less outside objects know about how and the more they only care about what the easier it is to understand code you or someone else wrote a year ago.

4) It centralizes control over the width property of that class which means that code is all in one place and easy to maintain. Any other way if something changed then code all over your program has to change, too, and that's just a big mess ;-)

5) The same goes with using setters instead of just shoving a number into Something.Width directly.

So basically it's a way of keeping the inside world of an object safe from the outside and making it really easy to change how an object works without disturbing the rest of your program.

Upvotes: 4

Related Questions