ArrayList.ToArray 方法

定义

ArrayList 的元素复制到新数组中。

重载

ToArray()

ArrayList 的元素复制到新 Object 数组中。

ToArray(Type)

ArrayList 的元素复制到新的指定元素类型数组中。

ToArray()

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

ArrayList 的元素复制到新 Object 数组中。

C#
public virtual object[] ToArray ();
C#
public virtual object?[] ToArray ();

返回

Object[]

一个包含 ArrayList 的元素副本的 Object 数组。

注解

元素是使用 Array.Copy复制的,这是一个 O(n) 操作,其中 nCount

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

ToArray(Type)

Source:
ArrayList.cs
Source:
ArrayList.cs
Source:
ArrayList.cs

ArrayList 的元素复制到新的指定元素类型数组中。

C#
public virtual Array ToArray (Type type);

参数

type
Type

要创建和复制元素的目标数组的元素 Type

返回

包含 ArrayList 的元素副本的指定元素类型数组。

例外

typenull

ArrayList 的类型无法自动转换为指定类型。

示例

下面的复制示例演示如何将 的 ArrayList 元素复制到字符串数组。

C#
using System;
using System.Collections;

public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "The" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList contains the following values:" );
      PrintIndexAndValues( myAL );

      // Copies the elements of the ArrayList to a string array.
      String[] myArr = (String[]) myAL.ToArray( typeof( string ) );

      // Displays the contents of the string array.
      Console.WriteLine( "The string array contains the following values:" );
      PrintIndexAndValues( myArr );
   }

   public static void PrintIndexAndValues( ArrayList myList )  {
      int i = 0;
      foreach ( Object o in myList )
         Console.WriteLine( "\t[{0}]:\t{1}", i++, o );
      Console.WriteLine();
   }

   public static void PrintIndexAndValues( String[] myArr )  {
      for ( int i = 0; i < myArr.Length; i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArr[i] );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList contains the following values:
        [0]:    The
        [1]:    quick
        [2]:    brown
        [3]:    fox
        [4]:    jumps
        [5]:    over
        [6]:    the
        [7]:    lazy
        [8]:    dog

The string array contains the following values:
        [0]:    The
        [1]:    quick
        [2]:    brown
        [3]:    fox
        [4]:    jumps
        [5]:    over
        [6]:    the
        [7]:    lazy
        [8]:    dog

*/

注解

对象中的所有 ArrayList 对象都将强制转换为 Type 参数中指定的 type

元素是使用 Array.Copy复制的,这是一个 O(n) 操作,其中 nCount

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0