Learner
Learner

Reputation: 4751

Please explain meaning/details of 'Properties are used for data binding; fields aren't.'

In this Why Properties Matter article, I found the following:

Properties are used for data binding; fields aren't.

I would like to know meaning of it. Can someone please elaborate it?

Upvotes: 1

Views: 322

Answers (2)

Dmitri Nesteruk
Dmitri Nesteruk

Reputation: 23798

Data binding requires that properties are able to notify the UI when they change. Fields can't do that by design. Properties, on the other hand, can do change notifications via suitable interfaces (e.g., INotifyPropertyChanged).

Upvotes: 2

Oded
Oded

Reputation: 499352

When you data bind a control, only the properties defined on the class you are binding can be used.

So, if your class looks like this:

public class MyClass
{
  public string aField = "something";

  public string aProperty { get; set;}

}

If you use data binding, you can use aProperty as a bound value, but not aField.

Read this overview of Data Binding on MSDN for more infomation.

Upvotes: 1

Related Questions