In this article, we will explore auto-properties and various ways to assign an initial value to it in C#.

To download the source code for this article, you can visit our GitHub repository.

The C# programming language supports the Auto-Properties feature from version 3.0. In almost every C# application we use auto-property because it enables a more concise way to declare the property.

What Is an Auto-Property in C#?

Auto-Properties in C# are used when no additional logic is required in the property accessors. We use this for those properties that don’t require add-on logic, and it uses 2 methods – get method and set method.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

Here, the set method assigns a value to the variable and the get method returns that value. 

Let’s look at the syntax to declare an auto-property in C#:

public int MyProperty { get; set; }

The public keyword is the access modifier, int is the data type and MyProperty is the name of the property. get and set are the accessors to get and set the value of the property.

If you want to know more about properties, check out our article C# Intermediate – Properties.

The Best Way to Assign Initial Value to Auto-Properties

Different C# versions have their approach for assigning values to auto-properties. So, let’s see some of the best ways to assign an initial value to an auto-property in C#.

Inline initialization for Auto-Properties

The first approach that we are going to cover is inline initialization, which was an approach introduced in C# version 6.0.

If the initial value is constant, then we should use inline initialization.

Let’s see this in action:

public class HondaCars
{
    public string Color { get; set; } = "White";
    public decimal Cost { get; set; } = 500000.00m;
}

Here, we define a class HondaCars with 2 properties, Color of type string and Cost of type decimal. Next, we assign values to properties similar to a normal variable in C#. So, in this way, we can directly assign the value to an auto-property.

Finally, we will access the properties of the HondaCars class:

var hondaCars = new HondaCars();
Console.WriteLine("===========Inline initialization==========");
Console.WriteLine($"Car Color: {hondaCars.Color}");
Console.WriteLine($"Car Cost: {hondaCars.Cost}");

Here, we create an instance of our HondaCars class to access the properties of the class and print out the properties Color and Cost to the console.

Let’s check out the output:

=============Inline initialization==================
Car Color: White
Car Cost: 500000.00

Constructor Initialization for Auto-Properties

C# version 3.0 and above can use constructor initialization to assign an initial value to an auto-property. So, let’s take a look at an approach that uses the constructor to set the initial value to an auto-property:

public class ToyotaCars
{
    public string Color { get; set; }
    public decimal Cost { get; set; }

    public ToyotaCars()
    {
        Color = "Black";
        Cost = 400000.00m;
    }
}

Here, we create a class ToyotaCars that contains 2 properties. Then, we create a constructor to assign the initial value to the properties.

So, let’s access the property of the class ToyotaCars by creating an object:

var toyotaCars = new ToyotaCars();
Console.WriteLine("======Constructor initialization==========");
Console.WriteLine($"Car Color: {toyotaCars.Color}");
Console.WriteLine($"Car Cost: {toyotaCars.Cost}");

Let’s check out the output:

=============Constructor initialization==================
Car Color: Black
Car Cost: 400000.00

Conclusion

So, in this article, we have learned what an auto-property is in C#, and how to set the initial value for an auto-property. Now, we know multiple ways to assign an initial value to an auto-property. But picking the best approach completely depends on our requirements in code.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!