In this program, a list of ints is created, and then the ReadOnlyCollection constructor is used with that List as an argument.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(3);
list.Add(5);
// Constructor.
ReadOnlyCollection<int> read = new ReadOnlyCollection<int>(list);
// Loop over ReadOnlyCollection.
foreach (int value in read)
{
Console.WriteLine(
"read: {0}", value);
}
// Copy ReadOnlyCollection to an array.
int[] array = new int[3];
read.CopyTo(array, 0);
// Display array.
foreach (int value in array)
{
Console.WriteLine(
"array: {0}", value);
}
// Use methods on ReadOnlyCollection.
int count = read.Count;
bool contains = read.Contains(-1);
int index = read.IndexOf(3);
Console.WriteLine(
"{0}, {1}, {2}", count, contains, index);
}
}
read: 1
read: 3
read: 5
array: 1
array: 3
array: 5
3, False, 1