Fields and properties look the same, but they are not. Properties are methods and as such there are certain things that are not supported for properties, and some things that may happen with properties but never in the case of fields.
Here’s a list of differences:
- Fields can be used as input to
out/refarguments. Properties can not.
- A field will always yield the same result when called multiple times (if we leave out issues with multiple threads). A property such as
DateTime.Nowis not always equal to itself.
- Properties may throw exceptions – fields will never do that.
- Properties may have side effects or take a really long time to execute. Fields have no side effects and will always be as fast as can be expected for the given type.
- Properties support different accessibility for getters/setters – fields do not (but fields can be made
- When using reflection the properties and fields are treated as different
MemberTypesso they are located differently (
- The JIT Compiler may treat property access very differently compared to field access. It may however compile down to identical native code but the scope for difference is there.