Keeping up with the K.I.S.S principle my previous post prompted an interesting debate in the #xna IRC channel.
Somebody commented
“User> Also, "public Vector2 Position { get; set; }" will transmit some bad habits to noobs. :]”
&
“<User> It expands it into a function call. If the get and set is public, it may as well be a public field.”
I missed most of the debate but here’s my take on things.
Why should we use auto properties as opposed to being maticulous and writing out the private variable? Or why bother with a property at all if you can’t be bothered to write the private variable and set and return it?
Firstly, I don’t see how it produces a bad habit. I come from a background where having to write a private accessor every time I needed a property was a very tedious task. Especially when dealing with hundreds of classes at the start of a large project, so this was a very welcome addition to .net for me as writing a private accessor just to return it and set it felt like a waste and left me with a ton of private variables I didn’t really need.
A few arguments for using them are.
- It follows the K.I.S.S principle.
- It looks a lot cleaner and has a smaller code footprint than writing a private property too.
- They’re so easy to implement using the Visual Studio Snippet feature. prop <tab> type <tab> name <enter>
- It allows you to maintain the API while giving you room for flexibility in the future to change the functionality of the get/set.
- If a third party relied on using reflection for API, fields are treat differently to properties.
So what really happens when we do use these auto properties.
Not surprisingly it does something very similar to what you would do if you write the private accessor yourself. Here’s an example.
Here is my own property with my own private variable. This took blood sweat and tears.
private int myvalue;
public int MyValue
{
get { return myValue; }
set { myValue = value; }
}
Here is my auto property for the lazy people.
public int MyValue { get; set; };
And here is what your code looks like after you compile an auto property.
[CompilerGenerated]
private int <myproperty>k__BackingField;
public int MyProperty
{
[CompilerGenerated]
get
{
return this.<myproperty>k__BackingField;
}
[CompilerGenerated]
set
{
this.<myproperty>k__BackingField = value;
}
}
It’s just doing the same thing, except adding an attribute to say it was generated by the compiler.
Over the course of a large application or a game, you would be amazed at how much time this saves, it keeps it simple and allows you to tighten up property get/set methods later in the development cycle.
There are a lot of other differences between Fields and Properties in c# but the main ones which effect this discussion are listed above.
Thanks to Björn for giving me the nudge to post.
This post is sponsered by Björn’s XNA Adventures
Completely agree with you! Its not as if you are hacking some code, its a .Net feature for goodness sake! The important thing is that it still provides you with the accessors for you to add code on the get/set IF you need to.