What is the benefits of using C# Properties over Fields

Many benefits-
1. Properties are used for data binding; Fields aren’t.
2. Encapsulation can be achieved with Properties, not with Fields. An example below-

public int Order
{
    get { return m_order; }
    set
    {
        //Encapsulation goes here
        // Put some rules checking here. Maybe a call to make sure that the order
        //isn't duplicated or some other error based on your business rules.
        m_order = value;
    }
}

3. Set part of Properties can raise notification events such as INotifyPropertyChanged.PropertyChanged which other objects can listen for and update the display value. This is important part of advanced data binding.

In short, its a new “Standard” of data storage that has advanced facilities than just mere storing the data in the members of class. By avoiding Properties typically you can perform all functions, but implementation may differ from person to person. Using Properties is a standard which helps everyone to define/ access/ validate/ notify the data storage in one single form.

This entry was posted in C#. Bookmark the permalink.

3 Responses to What is the benefits of using C# Properties over Fields

  1. I’m glad you like the articles. Thank you for commenting.

  2. Nice post! You truly have a wonderful way of writing which I find captivating! I will definitely be bookmarking you and returning to your blog. In fact, your post reminded me about a strange thing that happened to me the other day. Iâ??ll tell you about that laterâ?¦

  3. Thanks for any other informative blog. Where else could I am getting that type of information written in such a perfect approach? I have a mission that I’m just now working on, and I’ve been on the look out for such information.

Leave a Reply