Understanding Fields (Backing Fields) & Properties in C#

Alright, so I’m learning C# in detail now and somehow I’ve never really put into thought why something like a backing field was necessary or what a (backing) field really was until I was experimenting with Kotlin (a JVM language) for android development. The error occurred because Kotlin’s classes don’t allow backing fields and I was writing kotlin like java at the time which gave me that error. So now when getting back into C# I was trying to understand exactly what a backing field was and to my findings it’s a little simpler than what I was actually expecting. For me when I’m learning a new language I usually just accepted things as they are and a lot of newer programmers are like that. Rather than me understand why something works like it does and all the intricacies that comes with that language feature.

C# and I believe Java both have fields and properties. At first glance you would think they are the same. So what is a field? According to Microsoft’s definition, a field is a variable of any type that is declared directly in a class or struct. Now that’s a pretty generic description, it’s just a class variable right? Yeah, that’s exactly what a field is, it’s a variable that is defined and used within your classes.

Well then what the hell is a property? A property is just a field, usually with public visibility, with a fancy mechanism for accessing and writing data to a private variable. Why would I need that? Usually you don’t, but there are cool ways to introduce data conversions without using class methods to do so. So for example, say you have a database which stores unix timestamps right? Say you wanted to check what time it was an hour ago without modifying the timestamp or using class methods? This is when backing fields come into play. A backing field is just a field that is used by properties when you want to modify or use that private field data.  In other words, a property is just a reference to another private variable.  So to get back to our example:

So above we have a post object which takes a timestamp as a constructor parameter.  Our post object has a read only property, HourAgo, which gets the current timestamp and subtracts an hour from that timestamp.

Now you may or may not need backing fields.  You may just want automatic properties which generate backing fields for you and C#6 has you covered by simply calling:

public type property { get; set; } = value;

Which is pretty cool as well.  Some might say, well couldn’t I just create a class method?  Well yeah you could but properties can be a bit more powerful when it comes to dealing with data values especially with the set accessor property. Say using our example that we wanted to directly update the timestamp by passing a single digit which represents an hour using the HourAgo variable that subtracts exactly an hour from the current timestamp? Well we can modify our HourAgo variable and add a set property accessor to do just that:

set { timestamp -= (60 * 60) * value; }

Now if we called post.HourAgo = 1; it would subtract exactly 1 hour from the current timestamp.  Now picture how powerful this would be if we created say a music player with a feature like fast forward or rewind. We could create properties rather than using class methods which is extremely powerful.

Hopefully we should understand how Properties & Fields work and how they work together.