0

What is the difference between the following, and why would I choose the one above the other?

public string MyProperty { get; set; }

VS

public string MyProperty
{
    get { return _myProperty; }
    set { _myProperty = value; }
}
private string _myProperty;
Elmer
  • 384
  • 5
  • 19
  • 1
    No difference unless you wanted to handle `OnPropertyChange` or you wanted to manipulate `_myProperty` differently in `get` or `set`. Bonus, you can also do `MyProperty { get; private set; }` to only let owning class set the property value. – praty Oct 27 '17 at 06:08
  • Possible duplicate of https://stackoverflow.com/questions/15976031/difference-between-properties-with-get-set-and-without-get-set. – Tetsuya Yamamoto Oct 27 '17 at 06:09
  • @TetsuyaYamamoto that's not the same question.. – Fredrik Lundin Oct 27 '17 at 06:11
  • @TetsuyaYamamoto, the link shows difference in Field and Property. Here we have a question on difference in syntax of Property – praty Oct 27 '17 at 06:19

1 Answers1

0

Shorter syntax --> less bugs
Use get set with variables for complex operations

Shinigami
  • 646
  • 1
  • 7
  • 21
  • 4
    Should be a comment – praty Oct 27 '17 at 06:08
  • Please don't downvote without comment what is wrong on my answer – Shinigami Oct 27 '17 at 06:09
  • 1
    Not sure why it was down voted. But, I think the reason might be because you have enough credits to write a comment. The answer you have provided is not complete and is a small note. For it to be an answer, you must completely satisfy user's question. I believe there is more to it than just less bugs. – praty Oct 27 '17 at 06:17