This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

C# get/set property question

 
Ranch Hand
Posts: 598
3
jQuery Google App Engine 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.
 
Bartender
Posts: 4568
9
  • 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
jQuery Google App Engine Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good explanation. thank you, Matthew.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) One important design principle is that you never expose fields as public, but rather always access everything via properties. This is because you can never tell when a field is accessed and more importantly when it is set.
(2) Now, a lot of the time, there isn't any processing needed while setting or getting the value (for example, range checking).
     This is why Automatic Properties were created.
(3) In c#, a property is called as an auto-implemented property when it contains an accessors (get, set) without having any logic implementation.
(4) They are a simple, one-line way of creating a property. The backing store for it is created by the compiler.

(5) If you use an Automatic Property and later decide that you need to do something else in the set or get, you can easily change your code without breaking the public interface
(6) Automatic Properties are used when no additional logic is required in the property accessors..

Syntax::


They are just syntactic sugar so need not to write the following lengthy code:





 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!