possible duplicates:
should use public properties , private fields or public fields data?
difference between automatic properties , public field in c# 3.0
people seem dogmatically insist on use of public properties on fields why ultra-important in case of simple properties?
how
public int foo { get; set; } so incredibly different than
public int foo; ?
off top of head can think of few practical differences between two:
- accessing member using reflection (rare, , decent reflective algorithms account difference)
- the second entry allows use field valid parameter ref , out parameters, seem advantage using field version
- fields don't work in remoting (probably, i've never used remoting imagine wouldn't)?
other these rare cases, changing foo computed property later results in 0 lines of code changed.
using properties has couple of distinct advantages:
- it allows versioning if later need logic. adding logic getter or setter won't break existing code.
- it allows data binding work (most data binding frameworks don't work fields).
in addition, there no disadvantages. simple, automatic properties inlined jit compiler, there no reason not use them.
also, mentioned:
other these rare cases, changing foo computed property later results in 0 lines of code changed.
this doesn't require code changed, force recompile of code. changing field property breaking api change require assembly references assembly recompiled. making automatic property, can ship new binary, , maintain api compatibility. "versioning" advantage mentioned above...
Comments
Post a Comment