C# List AsReadOnly

Description

List AsReadOnly returns a read-only IList wrapper for the current collection.

Syntax

List.AsReadOnly has the following syntax.


public ReadOnlyCollection<T> AsReadOnly()

Example


using System;/*from  ww  w.  ja  v  a  2 s .  c o  m*/
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> myData = new List<string>(4);

        Console.WriteLine("\nCapacity: {0}", myData.Capacity);

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");

        IList<string> romyData = myData.AsReadOnly();

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack