Monthly Archives: April 2016

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.

What’s idiotCoder.com?

IdiotCoder is a new programming development/learning blog.  I’ve currently been a PHP developer for about 10 years now and I’ve decided that in order for me to grow as a programmer I need to move on from web development and go full forced into software and game development.  Web development has always been a passion and career of mine, but I’ve always wanted to be a game developer ever since I got into programming.  There’s a lot about programming that I don’t understand in languages such as C/C++, C# and Java, but there’s a lot I’ve tinkered with over the years while experimenting with those languages and others such as Kotlin, Nim, Go, Crystal and probably many more that have made me a better PHP developer.

I’ve learned that learning other languages can teach you how to think outside the box because each language has many different approaches and tools as well as limitations that other languages might not have like being functional only language or being class only language. You have to respect those limitations of what you have to solve those problems.

With that said you’ll see a lot of “no shit sherlock” posts which could be as simple as a few lines or longer thought out posts discussing or describing a finding while learning a language or parts of many languages.  I’m going to focus my efforts on posting things I had a hard time researching and finding a direct answer to so others can find that answer in a direct manner rather than a lot of articles beating around the bush on how something works.  That’s the true goal of this blog.  A learning experience for me and a reference for newer developers going through the same thing.