Difference Between Field and Property in C#

Difference Between Field and Property in C#

To understand the difference between a class property and a class field, let’s take their definitions.

 A Field  is a variable(usually private) of any type that is declared directly in a class or structure.
c# class field example
 A Property  is a member that provides a flexible mechanism to read, write, or compute the value of a private field(s).
C# class property examples

Based on the definition properties expose fields.  Properties provide some kind of wrapping allowing you to control the fields  while not affecting the external way they are accessed by the things that use your class.

In fact,  properties and fields are two very close concepts, but if you look at it, it will be clear that these are different things.  Usually, these two concepts are asked to be compared at job interviews(find more interview questions). And the purpose of this question is not so much to give definitions as to mislead the candidate.

But if you are persistently required to differ, then the following  differences can be voiced: 

  • Properties never store data
  • Interfaces can have properties but not fields
  • Following the rules of good code, properties have a public modifier and fields have a private one

Leave a Reply

Your email address will not be published. Required fields are marked *