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 to add read-only property in C#?
A read-only property in C# is a property that can only be set during object initialization and cannot be modified afterward. There are several ways to create read-only properties, including using the readonly keyword with fields, properties with only getters, and init-only properties (available in C# 9.0+).
Syntax
Following is the syntax for a read-only field −
readonly dataType fieldName;
Following is the syntax for a read-only property with only a getter −
public dataType PropertyName { get; }
Following is the syntax for an init-only property (C# 9.0+) −
public dataType PropertyName { get; init; }
Using Read-Only Fields
A field marked with readonly can only be set during object construction (in the constructor or at declaration). It cannot be changed afterward −
using System;
class Employee {
readonly int salary;
readonly string department = "IT"; // Set at declaration
public Employee(int salary) {
this.salary = salary; // Set in constructor
}
public void DisplayInfo() {
Console.WriteLine($"Salary: {salary}, Department: {department}");
}
public void UpdateSalary() {
// salary = 50000; // This would cause a compile error
Console.WriteLine("Cannot update read-only salary field");
}
}
class Program {
public static void Main() {
Employee emp = new Employee(45000);
emp.DisplayInfo();
emp.UpdateSalary();
}
}
The output of the above code is −
Salary: 45000, Department: IT Cannot update read-only salary field
Using Read-Only Properties with Get-Only Accessors
Properties with only getter accessors create read-only properties that can be set in the constructor or through expression-bodied members −
using System;
class Student {
public string Name { get; }
public int Id { get; }
public string FullInfo => $"{Id}: {Name}"; // Expression-bodied property
public Student(string name, int id) {
Name = name;
Id = id;
}
}
class Program {
public static void Main() {
Student student = new Student("Alice Johnson", 12345);
Console.WriteLine($"Name: {student.Name}");
Console.WriteLine($"ID: {student.Id}");
Console.WriteLine($"Full Info: {student.FullInfo}");
// student.Name = "Bob"; // This would cause a compile error
}
}
The output of the above code is −
Name: Alice Johnson ID: 12345 Full Info: 12345: Alice Johnson
Using Init-Only Properties (C# 9.0+)
Init-only properties allow setting values during object initialization but not afterward −
using System;
class Product {
public string Name { get; init; }
public decimal Price { get; init; }
public string Category { get; init; } = "General";
public void DisplayProduct() {
Console.WriteLine($"Product: {Name}, Price: ${Price}, Category: {Category}");
}
}
class Program {
public static void Main() {
Product product1 = new Product {
Name = "Laptop",
Price = 999.99m,
Category = "Electronics"
};
Product product2 = new Product {
Name = "Book",
Price = 19.99m
// Category uses default value "General"
};
product1.DisplayProduct();
product2.DisplayProduct();
// product1.Price = 899.99m; // This would cause a compile error
}
}
The output of the above code is −
Product: Laptop, Price: $999.99, Category: Electronics Product: Book, Price: $19.99, Category: General
Comparison of Read-Only Approaches
| Approach | When Set | C# Version | Use Case |
|---|---|---|---|
| readonly field | Constructor or declaration | All versions | Internal implementation details |
| Get-only property | Constructor only | C# 6.0+ | Public immutable properties |
| Init-only property | Object initialization | C# 9.0+ | Flexible immutable objects |
Conclusion
Read-only properties in C# ensure immutability by preventing modification after initialization. Use readonly fields for internal data, get-only properties for public immutable values, and init-only properties for flexible object initialization with immutability guarantees.
