In C# language one of the common interview questions is, "What isthe difference b/w Field and Property in C#?".
In this blog I will explain some differences between field and property in C#.
- public class Person
- {
- private int Id;
- public int User_ID
- {
-
- get
- {
- return Id;
- }
- set
- {
- Id = value;
- }
- }
- }
- Object orientated programming principles say that the internal workings of a class should be hidden from the outside world. If you expose a field you're in essence exposing the internal implementation of the class. So we wrap the field using the property to hide the internal working of the class.
We can see that in the above code section we define property as public and filed as private, so user can only access the property but internally we are using the field, such that provides a level of abstraction and hides the field from user access.
- Another important difference is that interfaces can have properties but not fields.
- Using property we can throw an event but this is not possible in field.
Example
- public class Person
- {
- private int Id;
- public int User_ID
- {
- get
- {
- return Id;
- }
- set
- {
- valueChanged();
- Id = value;
- }
- }
- public void valueChanged()
- {
-
- }
- }