When declaring an auto property for a class or struct in C#, the supporting field (a variable that holds the property’s value) can be defined through the auto property shortcut syntax. It makes setting up properties easier by automatically creating the core get
and set
accessors and the underlying field. It’s simple to build properties using auto properties rather than repeatedly writing the same code for supporting fields and basic accessors. It makes the entire code base more modular. They’re widely utilized for properties whose getters and setters don’t require any additional logic.
Different versions of C# have different methods of assigning values to an auto property. The new versions of C# support the syntax of previous versions for setting default values.
Note: The
get
method allows the programmer to access the value of the auto property, and it allows any external code outside theclass
to access the value of it.The
set
method allows the programmer to provide a value to the auto property, and it allows any external code outside theclass
to modify the value of it.If
get
andset
are not explicitly defined, the compiler is unable to distinguish between a basic field (such as variables inside a class) and an auto property. It would also not allow the user to set public or private accessors.
The code below is supported by C# versions 3.0 and newer. The auto property can be initialized using a constructor within the class
.
using System;class Car // A Class named "Car" is defined here{public string Name { get; set; } // The auto property "Name" is declared here.public Car() // Constructor for the class "Car"{Name = "Toyota Corolla"; //Setting the default value for "Name"}}class Program{static void Main(){Car car = new Car(); //Creating a new "Car" objectConsole.WriteLine($"Default Car: {car.Name}");}}
Lines 5–10: The constructor sets the initial value for the auto property Name
.
Therefore, initialization in the constructor is more versatile and dynamic.
Inline initialization is typically preferred when the initial value is constant and can be found during compilation.
using System;class Car // A Class named "Car" is defined here{public string Name { get; set; } = "Toyota Corolla";// The auto property "Name" is declared here and assigned a default value.}class Program{static void Main(){Car car = new Car(); //Creating a new "Car" objectConsole.WriteLine($"Default Car: {car.Name}");}}
Line 5: We give the auto property Name
a default value, Toyota Corolla
.
Line 12: When establishing a new Car
object, the Name
property’s default value of Toyota Corolla
is used if no explicit value is provided to it.
C# versions 9.0 and newer, allow us to use a property initializer. This allows the programmer to set a default/initial value per their logic. The code provided below gives an example of this.
using System;class Car // A Class named "Car" is defined here{public string Name { get; set; } = InitializePropertyValue();// InitializePropertyValue() is called here to set the "Name" auto property.private static string InitializePropertyValue()// Private static method to return a default value for "Name"{return "Toyota Corolla";}}class Program{static void Main(){Car car = new Car(); //Creating a new "Car" objectConsole.WriteLine($"Default Car: {car.Name}");}}
Lines 5–13: We call the InitializePropertyValue()
method to initialize the value of the object car
to the given default value of the Toyota Corolla
.
Property initialization is useful when value initialization requires extensive computation.
We’ve learned how the auto property can be given an initial/default value in different versions of C#.
Free Resources