This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Other Languages and the fly likes C# get/set property question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "C# get/set property question " Watch "C# get/set property question " New topic
Author

C# get/set property question

Bobby Sharma
Ranch Hand

Joined: Mar 18, 2008
Posts: 574
    
    1

I don't know difference between these two code snippet.


and this one.



please explain.


Back to Java , again.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

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

Joined: Mar 18, 2008
Posts: 574
    
    1

good explanation. thank you, Matthew.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: C# get/set property question