The following example demonstrates the simple example of the ReadOnlyCollection
List<string> citiesList = new List<string>(); citiesList.Add("NewDelhi"); citiesList.Add("Londan"); citiesList.Add("NewYork"); citiesList.Add("Tokyo"); ReadOnlyCollection<string> readOnlycities = new ReadOnlyCollection<string>(citiesList);
Get the Count of the items in the ReadOnlyCollection
With the help of the Count property we can getthe number of elements contained in the ReadOnlyCollection
Console.WriteLine("\nCount: {0}", readOnlycities.Count);
ReadOnlyCollection
the code example shows that the ReadOnlyCollection
cities.Insert(3, "Paris"); foreach (string city in readOnlycities) { Console.WriteLine(city); }
The result will be:
NewDelhi
Londan
NewYork
Paris
Tokyo
The object of the ReadOnlyCollection