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
Final local variables in C#
In C#, there is no direct equivalent to Java's final keyword for local variables. However, you can achieve similar behavior using the readonly keyword for fields and implicit typing with var or const for local variables that should not change after initialization.
The readonly keyword allows a field to be assigned a value only once − either at the time of declaration or in the constructor. Once assigned, it cannot be modified.
Syntax
Following is the syntax for declaring a readonly field −
readonly dataType fieldName;
For local variables that should remain constant, use const −
const dataType variableName = value;
Using Readonly Fields
A readonly field can only be assigned in two places: at declaration or in a constructor. This makes it ideal for creating immutable fields that are set once during object creation −
using System;
class Department {
readonly int empCount;
readonly string deptName;
public Department(int empCount, string deptName) {
this.empCount = empCount;
this.deptName = deptName;
}
public void DisplayInfo() {
Console.WriteLine($"Department: {deptName}, Employees: {empCount}");
}
public void ChangeCount() {
// empCount = 150; // This would cause a compile error
// Console.WriteLine("Cannot modify readonly field");
}
}
class Program {
public static void Main() {
Department dept = new Department(100, "IT");
dept.DisplayInfo();
}
}
The output of the above code is −
Department: IT, Employees: 100
Using Const for Local Variables
For local variables that should not change, use const. These must be assigned at compile time with constant expressions −
using System;
class MathOperations {
public static void CalculateCircleArea(double radius) {
const double PI = 3.14159; // Cannot be changed
double area = PI * radius * radius;
Console.WriteLine($"Radius: {radius}");
Console.WriteLine($"Area: {area}");
// PI = 3.14; // This would cause a compile error
}
public static void Main() {
CalculateCircleArea(5.0);
}
}
The output of the above code is −
Radius: 5 Area: 78.5395
Readonly vs Const Comparison
| Readonly | Const |
|---|---|
| Can be assigned at runtime in constructor | Must be assigned at compile time |
| Works with instance and static fields | Always static by default |
| Can hold reference types | Limited to compile-time constants |
| Value can differ per instance | Same value across all instances |
Readonly with Reference Types
When using readonly with reference types, the reference itself cannot be changed, but the object it points to can still be modified −
using System;
using System.Collections.Generic;
class Company {
readonly List<string> employees;
public Company() {
employees = new List<string>();
}
public void AddEmployee(string name) {
employees.Add(name); // This is allowed
}
public void DisplayEmployees() {
foreach(string emp in employees) {
Console.WriteLine($"Employee: {emp}");
}
}
public void ResetEmployees() {
// employees = new List<string>(); // This would cause compile error
employees.Clear(); // But this is allowed
}
}
class Program {
public static void Main() {
Company comp = new Company();
comp.AddEmployee("John");
comp.AddEmployee("Jane");
comp.DisplayEmployees();
}
}
The output of the above code is −
Employee: John Employee: Jane
Conclusion
While C# doesn't have a final keyword for local variables, readonly fields and const local variables provide similar immutability guarantees. Use readonly for fields that should be set once during construction, and const for compile-time constant values.
