"읽기 전용"으로 표시된 필드는 개체를 구성하는 동안 한 번만 설정할 수 있습니다. 변경할 수 없습니다 -
예를 들어 보겠습니다.
class Employee {
readonly int salary;
Employee(int salary) {
this.salary = salary;
}
void UpdateSalary() {
//salary = 50000; // Compile error
}
} 위에서 급여 필드를 읽기 전용으로 설정했습니다.
변경하면 컴파일 타임 오류가 발생합니다. 위의 예에서도 마찬가지입니다.
이제 배열이 읽기 전용인지 여부를 확인하는 방법을 살펴보겠습니다. −
예
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lower {
class Program {
static void Main(string[] args) {
Array arr = Array.CreateInstance(typeof(String), 3);
arr.SetValue("Maths", 0);
arr.SetValue("Science", 1);
arr.SetValue("PHP", 2);
Console.WriteLine("isReadOnly: {0}",arr.IsReadOnly.ToString());
}
}
}