System.Collections.ArrayList IsReadOnly Property
How to create a read-only copy of the ArrayList
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class Program { static void Main(string[] args) { ArrayList colors = new ArrayList() { "Salmon", "SandyBrown", "Gray" }; Console.WriteLine("ArrayList Elements... "); foreach (string color in colors) { Console.WriteLine(color); } ArrayList readOnlyColors = ArrayList.ReadOnly(colors); Console.WriteLine("\nIs 'colors' ArrayList ReadOnly? " + colors.IsReadOnly.ToString()); Console.WriteLine("Is 'readOnlyColors' ArrayList ReadOnly? " + readOnlyColors.IsReadOnly.ToString()); colors.Add("White"); //Uncomment this line to get error message. //readOnlyColors.Add("White"); Console.WriteLine("\n<font color=DodgerBlue>After Adding 'White', Now 'colors' ArrayList Elements... "); foreach (string color in colors) { Console.WriteLine(color); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
ArrayList Elements... Salmon SandyBrown Gray Is 'colors' ArrayList ReadOnly? False Is 'readOnlyColors' ArrayList ReadOnly? True <font color=DodgerBlue>After Adding 'White', Now 'colors' ArrayList Elements... Salmon SandyBrown Gray White |