Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Auto-implemented Properties in C#

Let's take a look at auto-implemented properties and see how it changed the lives of many developers for the better.

Jan 30, 2020 • 4 Minute Read

Introduction

Back in the days before version 3 of C# was released, there was a big dilemma among programmers who used the object-oriented programming paradigm. The paradigm goes like this: Properties should be get-set mappings of private and public fields. This additional layer is used as per guidelines. Most of the folks out there resisted the urge to simply declare public fields and use them.

In this guide, we will take a look at auto-implemented properties, a new feature of C# that was introduced with version 3, and see how it changed the lives of many developers for the better.

Automatic Properties

We'll start by declaring a simple class in the following example. This class represents a network device which will have a name, a location, and a serial number.

      using System;
namespace Pluralsight
{
    public class NetworkDevice
    {
        private string name;
        private string location;
        private string serial;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string Location
        {
            get { return location; }
            set { location = value; }
        }
        public string Serial
        {
            get { return serial; }
            set { serial = value; }
        }
        public static void Main()
        {
            NetworkDevice sw = new NetworkDevice();
            sw.Name = "L2 switch";
            sw.Location = "Amsterdam";
            sw.Serial = "S1212323";
            Console.WriteLine($"The device's name is {sw.Name}, the location is {sw.Location}, the serial is {sw.Serial}");
            Console.ReadKey();
        }

    }
}
    

Executing the code gives us the following result.

      The device's name is L2 switch, the location is Amsterdam, the serial is S1212323
    

The code demonstrates how the implementation worked before there were auto-implemented properties, as object-oriented programming recommends. The code is obfuscated a little bit by the additional layer that is a mapping between the public properties and private ones. Since version 3 of C#, this dilemma is not present anymore. We can skip the lines with private, and we don't need the definitions for the get-set, either.

This way, our code becomes cleaner.

      using System;
namespace Pluralsight
{
    public class NetworkDevice
    {
        public string Name { get; set; }
        public string Location { get; set; }
        public string Serial { get; set; }
        public static void Main()
        {
            NetworkDevice sw = new NetworkDevice();
            sw.Name = "L2 switch";
            sw.Location = "Amsterdam";
            sw.Serial = "S1212323";
            Console.WriteLine($"The device's name is {sw.Name}, the location is {sw.Location}, the serial is {sw.Serial}");
            Console.ReadKey();
        }

    }
}
    

The output is the same.

Conclusion

All in all, the introduction of this new feature helped people reorganize their code. The result was maintainable code that was easy to understand. Now we don't need to use field declaration or any code for the get-set methods to modify the field. This is handled automatically by the compiler. Behind the curtain, the compiler creates the private field and ensures the mapping, and it also populates the get-set methods to allow read and write of the specific fields.

I hope this guide has been informative to you and I would like to thank you for reading it!