Instead of classic class field definition, C# adds one more class element -
property. Sometimes there is hard to understand difference between field and property in C# for beginners.
Main difference is:
Field is just simple variable within class, while property is field wrapped in two functions: getter and setter.
So in general, if you define property, compiler silently creates invisible field for that property make that field accessible by get and set functions which are wrapped in property name. Thanks to that we can use properties like public fields in our code with encapsulation keeping.
Let's see below example:
Output of that example is:
In point I we are defining normal field - private field, because '
private' is default protection level in C#.
In point II we are defining default property. As we can see we have two keywords (
get and
set) within definition of that property.
Those keywords defines default getter and setter for out property.
Thanks to that we can
use that property as public field (encapsulation is being kept thanks to
get and
set functions within property definition) - see points V and VII.
What is more we can customize property get and set functions according to our needs.
Take a look at point III. We are defining there
TestFieldProperty which has customized get and set function. That property is related to our private
testField, so we can use it to modify
testField according to our needs.
In this example when we are using assignment operator for our
TestFieldProperty assigning its value 10 as in point VI, we are actually assigning value 20 to our
testField.
Thus, when we are getting value of
TestFieldProperty in point VIII, it will return value 20 (because it is actual value of
testField according to our customization of get function in TestFieldProperty).
Code of above example you can find on github repository related to this blog here:
https://github.com/xmementoit/CSharpAdventureExamples/tree/master/csharpBasics/fieldPropertyDifference