Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General 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: 4567
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