当前位置: 首页>>代码示例>>C#>>正文


C# ArrayList.IsReadOnly属性代码示例

本文整理汇总了C#中System.Collections.ArrayList.IsReadOnly属性的典型用法代码示例。如果您正苦于以下问题:C# ArrayList.IsReadOnly属性的具体用法?C# ArrayList.IsReadOnly怎么用?C# ArrayList.IsReadOnly使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Collections.ArrayList的用法示例。


在下文中一共展示了ArrayList.IsReadOnly属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
 using System.Collections;
 public class SamplesArrayList  {
 
    public static void Main()  {
 
       // Creates and initializes a new ArrayList.
       ArrayList myAL = new ArrayList();
       myAL.Add( "red" );
       myAL.Add( "orange" );
       myAL.Add( "yellow" );
 
       // Creates a read-only copy of the ArrayList.
       ArrayList myReadOnlyAL = ArrayList.ReadOnly( myAL );
 
       // Displays whether the ArrayList is read-only or writable.
       Console.WriteLine( "myAL is {0}.", myAL.IsReadOnly ? "read-only" : "writable" );
       Console.WriteLine( "myReadOnlyAL is {0}.", myReadOnlyAL.IsReadOnly ? "read-only" : "writable" );
 
       // Displays the contents of both collections.
       Console.WriteLine( "\nInitially," );
       Console.WriteLine( "The original ArrayList myAL contains:" );
       foreach ( String myStr in myAL )
          Console.WriteLine( "   {0}", myStr );
       Console.WriteLine( "The read-only ArrayList myReadOnlyAL contains:" );
       foreach ( String myStr in myReadOnlyAL )
          Console.WriteLine( "   {0}", myStr );

       // Adding an element to a read-only ArrayList throws an exception.
       Console.WriteLine( "\nTrying to add a new element to the read-only ArrayList:" );
       try  {
          myReadOnlyAL.Add("green");
       } catch ( Exception myException )  {
          Console.WriteLine("Exception: " + myException.ToString());
       }

       // Adding an element to the original ArrayList affects the read-only ArrayList.
       myAL.Add( "blue" );

       // Displays the contents of both collections again.
       Console.WriteLine( "\nAfter adding a new element to the original ArrayList," );
       Console.WriteLine( "The original ArrayList myAL contains:" );
       foreach ( String myStr in myAL )
          Console.WriteLine( "   {0}", myStr );
       Console.WriteLine( "The read-only ArrayList myReadOnlyAL contains:" );
       foreach ( String myStr in myReadOnlyAL )
          Console.WriteLine( "   {0}", myStr );
    }
 }
开发者ID:.NET开发者,项目名称:System.Collections,代码行数:50,代码来源:ArrayList.IsReadOnly

输出:

myAL is writable.
myReadOnlyAL is read-only.

Initially,
The original ArrayList myAL contains:
   red
   orange
   yellow
The read-only ArrayList myReadOnlyAL contains:
   red
   orange
   yellow

Trying to add a new element to the read-only ArrayList:
Exception: System.NotSupportedException: Collection is read-only.
   at System.Collections.ReadOnlyArrayList.Add(Object obj)
   at SamplesArrayList.Main()

After adding a new element to the original ArrayList,
The original ArrayList myAL contains:
   red
   orange
   yellow
   blue
The read-only ArrayList myReadOnlyAL contains:
   red
   orange
   yellow
   blue

示例2: Main

//引入命名空间
using System;
using System.Collections;

class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    
    Console.WriteLine("myArrayList.IsFixedSize = " + myArrayList.IsFixedSize);
    Console.WriteLine("myArrayList.IsReadOnly = " + myArrayList.IsReadOnly);

  }
}
开发者ID:C#程序员,项目名称:System.Collections,代码行数:15,代码来源:ArrayList.IsReadOnly


注:本文中的System.Collections.ArrayList.IsReadOnly属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。