"रीड-ओनली" मार्क किया गया फील्ड, ऑब्जेक्ट के निर्माण के दौरान केवल एक बार सेट किया जा सकता है। इसे बदला नहीं जा सकता:
आइए एक उदाहरण देखें।
class Employee { readonly int salary; Employee(int salary) { this.salary = salary; } void UpdateSalary() { //salary = 50000; // Compile error } }उपरोक्त, हमने salary फील्ड को रीड-ओनली पर सेट किया है।
यदि आप इसे बदलेंगे, तो कंपाइल-टाइम त्रुटि होगी। वही उपरोक्त उदाहरण में दिखाया गया है।
आइए अब देखें कि कैसे जांचें कि कोई ऐरे रीड-ओनली है या नहीं:
उदाहरण
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()); } } }