Author avatar

Dániel Szabó

Auto-implemented Properties in C#

Dániel Szabó

  • Jan 30, 2020
  • 4 Min read
  • 658 Views
  • Jan 30, 2020
  • 4 Min read
  • 658 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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();
        }

    }
}
csharp

Executing the code gives us the following result.

1
The 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
        }

    }
}
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!

8