-1

Possible Duplicate:
Properties vs Methods

I'm a beginner C# programmer and recently discovered how to use properties to expose members. However I'm confused on when to use a property as apposed to a method when returning something.

Should I do this:

public Vector2 Center {
    get {
        Vector2 screenDem = new Vector2(game.GraphicsDevice.Viewport.Width,
            game.GraphicsDevice.Viewport.Height);
        return new Vector2(screenDem.X / 2, screenDem.Y / 2);
    }
}

or should I do something like this:

public Vector2 GetScreenCenter() {
    Vector2 screenDem = new Vector2(game.GraphicsDevice.Viewport.Width,
            game.GraphicsDevice.Viewport.Height);
    return new Vector2(screenDem.X / 2, screenDem.Y / 2);
}

When should I use which and why?

Maybe I'm just thinking about this too hard and it doesn't matter, I don't know.

Thanks.

Community
  • 1
  • 1
MrPlosion1243
  • 67
  • 1
  • 5
  • @KirkWoll. It's clearly not a dup, this question is _properties **or** methods_, while your link is _properties **vs** methods_. Get real! `:)` – gdoron Jun 19 '12 at 20:51
  • At the end of the day, your 2 approaches are equivalent. – blearn Jun 19 '12 at 20:49

1 Answers1

2

If you just want to get and set a value then a property is best.

public DateTime TheCurrentTime {get; set;}

If you need to pass parameters in order to modify or return something then a method is best.

public DateTime HowManyDaysUntilMyBirthday(Datetime MyBirthday) 
{
    return (MyBirthday - DateTime.Now());
}
John Mitchell
  • 9,653
  • 9
  • 57
  • 91