Author avatar

Dániel Szabó

Auto-implemented Properties in C#

Dániel Szabó

  • Jan 30, 2020
  • 4 Min read
  • 6,271 Views
  • Jan 30, 2020
  • 4 Min read
  • 6,271 Views
Languages Frameworks and Tools
C#

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.

1using System;
2namespace Pluralsight
3{
4    public class NetworkDevice
5    {
6        private string name;
7        private string location;
8        private string serial;
9        public string Name
10        {
11            get { return name; }
12            set { name = value; }
13        }
14        public string Location
15        {
16            get { return location; }
17            set { location = value; }
18        }
19        public string Serial
20        {
21            get { return serial; }
22            set { serial = value; }
23        }
24        public static void Main()
25        {
26            NetworkDevice sw = new NetworkDevice();
27            sw.Name = "L2 switch";
28            sw.Location = "Amsterdam";
29            sw.Serial = "S1212323";
30            Console.WriteLine($"The device's name is {sw.Name}, the location is {sw.Location}, the serial is {sw.Serial}");
31            Console.ReadKey();
32        }
33
34    }
35}
csharp

Executing the code gives us the following result.

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

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.

1using System;
2namespace Pluralsight
3{
4    public class NetworkDevice
5    {
6        public string Name { get; set; }
7        public string Location { get; set; }
8        public string Serial { get; set; }
9        public static void Main()
10        {
11            NetworkDevice sw = new NetworkDevice();
12            sw.Name = "L2 switch";
13            sw.Location = "Amsterdam";
14            sw.Serial = "S1212323";
15            Console.WriteLine($"The device's name is {sw.Name}, the location is {sw.Location}, the serial is {sw.Serial}");
16            Console.ReadKey();
17        }
18
19    }
20}
csharp

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!