Fields and Properties

  • Thread starter Thread starter Claude Grecea
  • Start date Start date
C

Claude Grecea

Can someone please explain to me when you would use a Property instead of a
field and wy? Thanks in advance.
 
Can someone please explain to me when you would use a Property instead of a
field and wy? Thanks in advance.

Hi Claude,

A few things that springs to mind

1) DataBinding requires a Property
2) Smart fields. Processing indata before storing it, or not storing data at all but generating the value when asked.
3) Better control over who can write to the property. You can have public getters and private setters.
4) Easier to change logic. You can rewrite the getter and setter without touching outside code.

I don't think I have exposed a class field for a long time, and rarely even use the fields other than as property storage. With C# 3.0 even property storage isn't needed in many cases.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top