Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do you give a C# Auto-Property a default value?
In C#, auto-properties provide a shorthand way to declare properties without explicitly writing getter and setter methods. Setting default values for auto-properties can be done in two main ways: using constructor initialization (available in all C# versions) or using property initializers (introduced in C# 6.0).
Syntax
Constructor initialization syntax (C# 5.0 and earlier) −
public class ClassName {
public PropertyType PropertyName { get; set; }
public ClassName() {
PropertyName = defaultValue;
}
}
Property initializer syntax (C# 6.0 and later) −
public class ClassName {
public PropertyType PropertyName { get; set; } = defaultValue;
}
Using Constructor Initialization
In C# 5.0 and earlier versions, auto-properties could only be initialized in the constructor. The constructor is automatically called when the class is instantiated, setting the property value −
Example
using System;
class Demo {
public Demo() {
FirstName = "DemoName";
Age = 25;
}
public string FirstName { get; set; }
public int Age { get; set; }
}
class Program {
static void Main() {
Demo obj = new Demo();
Console.WriteLine("Name: " + obj.FirstName);
Console.WriteLine("Age: " + obj.Age);
}
}
The output of the above code is −
Name: DemoName Age: 25
Using Property Initializers (C# 6.0+)
C# 6.0 introduced property initializers, allowing you to set default values directly at the property declaration. This approach is more concise and eliminates the need for constructor initialization for simple default values −
Example
using System;
class Demo {
public string FirstName { get; set; } = "DemoName";
public int Age { get; set; } = 25;
public bool IsActive { get; set; } = true;
}
class Program {
static void Main() {
Demo obj = new Demo();
Console.WriteLine("Name: " + obj.FirstName);
Console.WriteLine("Age: " + obj.Age);
Console.WriteLine("Active: " + obj.IsActive);
}
}
The output of the above code is −
Name: DemoName Age: 25 Active: True
Combining Both Approaches
You can use both property initializers and constructor initialization together. The constructor will override the property initializer values −
Example
using System;
class Demo {
public string FirstName { get; set; } = "DefaultName";
public int Age { get; set; } = 18;
public Demo(string name) {
FirstName = name; // Override the default
}
}
class Program {
static void Main() {
Demo obj1 = new Demo("CustomName");
Console.WriteLine("Name: " + obj1.FirstName + ", Age: " + obj1.Age);
}
}
The output of the above code is −
Name: CustomName, Age: 18
Comparison
| Method | C# Version | Advantages | Best Use Case |
|---|---|---|---|
| Constructor Initialization | All versions | Conditional logic, complex initialization | Dynamic defaults based on parameters |
| Property Initializers | 6.0+ | Concise, readable, no constructor needed | Simple constant default values |
Conclusion
Auto-properties can be given default values using constructor initialization (all C# versions) or property initializers (C# 6.0+). Property initializers offer cleaner syntax for simple defaults, while constructor initialization provides more flexibility for complex scenarios.
