We help IT Professionals succeed at work.

Field vs Property?

paulwhelan
paulwhelan asked
on
Medium Priority
271 Views
Last Modified: 2010-04-16
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
Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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

Commented:
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.
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.

Commented:
One more reason for properties is the use of Interfaces.

A Interface can not contain fields, but it can contain Properties.
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
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.

Author

Commented:
Thanks guys!

So I need to use both properties and fields?

Cheers
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
Yes. Fields are the (usually) data storage that are accessed by the properties.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.