📜  C#|为列表创建只读包装器

📅  最后修改于: 2021-05-30 01:06:49             🧑  作者: Mango

List .AsReadOnly方法用于获取当前集合的只读ReadOnlyCollection 包装器。

句法:

返回值:返回一个对象,该对象充当当前List 周围的只读包装器。

例子:

// C# code to create a read-only
// wrapper for the List
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an List of Integers
        List mylist = new List();
  
        // Adding elements to List
        mylist.Add(17);
        mylist.Add(19);
        mylist.Add(21);
        mylist.Add(9);
        mylist.Add(75);
        mylist.Add(19);
        mylist.Add(73);
  
        Console.WriteLine("Before Wrapping: ");
  
        // Displaying the elements
        // in the mylist
        foreach(int i in mylist)
        {
            Console.WriteLine(i);
        }
  
        // Creating a Read-Only packing
        // around the List
        IList readlist = mylist.AsReadOnly();
  
        Console.WriteLine("After Wrapping: ");
  
        // Displaying the elements
        // of Read-Only Collection
        foreach(int j in readlist)
        {
            Console.WriteLine(j);
        }
          
        // You can add elements to
        // the original List i.e. mylist
        Console.WriteLine("Adding new element to mylist: ");
        mylist.Add(35);
          
        // Displaying the elements
        // in the mylist
        foreach(int k in mylist)
        {
            Console.WriteLine(k);
        }
  
        // But you cannot add elements
        // into the Read-Only Collection
        Console.WriteLine("Trying to add new element into readlist:");
  
        // it will give error
        readlist.Add(34);
          
    }
}

输出:

Before Wrapping: 
17
19
21
9
75
19
73
After Wrapping: 
17
19
21
9
75
19
73
Adding new element to mylist: 
17
19
21
9
75
19
73
35
Trying to add new element into readlist:

运行时错误:

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.asreadonly?view=netframework-4.7.2