James Michael Hare

...hare-brained ideas from the realm of software development...
posts - 166 , comments - 1431 , trackbacks - 0

My Links


Welcome to my blog! I'm a Sr. Software Development Engineer in the Seattle area, who has been performing C++/C#/Java development for over 20 years, but have definitely learned that there is always more to learn!

All thoughts and opinions expressed in my blog and my comments are my own and do not represent the thoughts of my employer.

Blogs I Read

Follow BlkRabbitCoder on Twitter

Tag Cloud


Post Categories



Little Wonders

Little Wonders


C#/.NET Little Wonders: Auto-property initialization in C# 6

Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, but can help improve your code by making it easier to write and maintain. The index of all my past little wonders posts can be found here. 

Visual Studio 2015 is on the horizon!  In fact, some of you may already have played with the preview and seen some of the many neat new things to come – both in the IDE and in the C# language.

For those who haven’t been keeping up with the announcements, allow me to take some blog time for the next few Little Wonders posts to talk about some of the neat new things that will be part of C# 6. 

Then, once it’s available for you to consume, you can hit the ground running with an arsenal of new little ways to make your code cleaner and more maintainable.

Auto-Property Initialization

Remember than in C#, auto-properties are a way to create the boilerplate code for a basic, field-backed property in C#. 

For instance, if you wanted to create a Point class with members X, and Y you used to have to define that with code like this:

   1: public class Point
   2: {
   3:     private int x;
   4:     private int y;
   6:     public int X
   7:     {
   8:         get { return x; }
   9:         set { x = value; }
  10:     }
  12:     public int Y
  13:     {
  14:         get { return y; }
  15:         set { y = value; }
  16:     }
  17: }

The developers on the C# team realized that this was really just standard boiler-plate for accessors and mutators.  You were simply defining a backing field with the get returning the field and the set assigning the field, so they allowed a syntactical shortcut called auto-properties:

   1: public class Point
   2: {
   3:     public int X { get; set; }
   5:     public int Y { get; set; }
   6: }

Essentially, these two classes are nearly identical behind the scenes.  Both still have the backing fields, but in the second example the compiler is the one creating the backing field and naming it and hiding all that boilerplate code for you.  You are simply saying in the { get; set; } that you wish for it to generate a getter and a setter.

Similarly, if you want the setter to be private (like in an immutable class) you can modify the access for either the set to make it different:

   1: // Make Point immutable by passing value in constructor and setting privately
   2: public class Point
   3: {
   4:     // setters now private
   5:     public int X { get; private set; }
   7:     public int Y { get; private set; }
   9:     public Point(int x, int y)
  10:     {
  11:         X = x;
  12:         Y = y;
  13:     }
  14: }

This is all well and good, but there has been for quite some time a disparity between VB.NET and C#.NET around auto-properties.  Namely, VB.NET allowed you to assign a non-default initialization value to an auto-property, but C# did not.

For example, let’s say you are creating a resource pool and you want an auto-property for the pool size, but you want it to default to 10, not 0.  You used to have to do this by setting the value in the constructor, (or by abandoning the auto-property and creating a field):

   1: public class ResourcePool
   2: {
   3:     // couldn't initialize an auto-property before
   4:     int PoolSize { get; set; }
   6:     // would have to set in constructor if you wanted a different value on construction
   7:     public ResourcePool()
   8:     {
   9:         PoolSize = 10;
  10:     }
  12:     // ...
  13: }

With VB.NET, they’ve had the ability to initialize an auto-property for a while now.  But finally, with C# 6.0, we get that feature as well. 

This means the code above could now look like this:

   1: public class ResourcePool
   2: {
   3:     // can now set an initialization value, no longer have to set in constructor
   4:     int PoolSize { get; set; } = 10;
   6:     // ...
   7: }

Notice that now, you can assign an initialization value to the property right after the closing brace.  Simple and intuitive!

This also means that you can create immutable properties without needing a setter, for instance if we wanted to add a MaxPoolSize that would be exposed as a property (instead of an exposed const field):

   1: public class ResourcePool
   2: {
   3:     // true read-only property, has no setter 
   4:     public static int MaxPoolSize { get; } = 50;
   6:     // ...
   7: }

Notice that there is no setter at all (not even private).  Thus once this field is created it keeps its initial value permanently.


C# 6.0 is going to add several Little Wonders that can help make your code cleaner and easier to maintain.  Stay tuned next time (in between my puzzlers) for more up-and-coming C# 6.0 goodness!

Print | posted on Thursday, March 19, 2015 4:19 PM | Filed Under [ My Blog C# Software .NET Little Wonders vNext ]

Powered by: