This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

C# get/set property question

 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know difference between these two code snippet.


and this one.



please explain.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first version uses "automatic properties", which are syntactic sugar: if you want a simple getter/setter pair you can use this and the private backing variable will be automatically declared (and hidden from you) - it's a short-cut.

Which means that the example isn't right - you shouldn't also declare private variables. The correct idiom is:
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good explanation. thank you, Matthew.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic