Field vs Property?

Hi
whats the difference between a Field and a Property?

If I can modify a Field through a method then isn't that just the same thing?

Thanks
Paul
paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
A field technically refers to a variable stored inside an object. Rather than simply making the field public you usually use properties to give you more control over how your fields are accessed. For example, using a property you can make a field read or write only, you can also perform validation when someone tries to set the value, or the value returned by the property could be calculated at runtime.

Plus if you decide to change the type of a field, or how it is stored, you can do so without breaking anything that consumes your object.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
existenz2Commented:
In short:

public string test = ""; // Field

public string Test { // Property
 get {
  return test;
}
set {
  test = value;
}
}

The advantage from using properties are that you:
A) can define if people are allowed to get/set data (both are optional)
B) can do validation before you set/get the data
C) can perform custom things before data is get/set
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

woiglCommented:
To the example before i would suggest to use the field private, otherwise you give full access outside the class...

private string test = ""; // Field

public string Test { // Property
get {
  return test;
}
set {
  test = value;
}
}

Even internal is the handling of the fields and properties different.

this kind of get and set functions are much better handled compare to own written functions to control get and set.
0
existenz2Commented:
I agree with woigl that in cases when you want to have fields available to outside the class you should make use of properties instead of public fields.
0
woiglCommented:
One more reason for properties is the use of Interfaces.

A Interface can not contain fields, but it can contain Properties.
0
Carl TawnSystems and Integration DeveloperCommented:
Also, while we're covering all bases, you need to use properties if you want to support databinding, as WinForms controls can only bind to properties and not directly to fields.
0
paulwhelanAuthor Commented:
Thanks guys!

So I need to use both properties and fields?

Cheers
0
Carl TawnSystems and Integration DeveloperCommented:
Yes. Fields are the (usually) data storage that are accessed by the properties.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.