次の方法で共有


Array.CreateInstance メソッド (Type, Int32)

Type と長さを指定して、0 から始まるインデックス番号を持つ 1 次元の Array を作成します。

Overloads Public Shared Function CreateInstance( _
   ByVal elementType As Type, _   ByVal length As Integer _) As Array
[C#]
public static Array CreateInstance(TypeelementType,intlength);
[C++]
public: static Array* CreateInstance(Type* elementType,intlength);
[JScript]
public static function CreateInstance(
   elementType : Type,length : int) : Array;

パラメータ

  • elementType
    作成する ArrayType
  • length
    作成する Array のサイズ。

戻り値

指定した Type と長さの、0 から始まるインデックス番号を持つ新しい 1 次元の Array

例外

例外の種類 条件
ArgumentNullException elementType が null 参照 (Visual Basic では Nothing) です。
ArgumentException elementType が有効な Type ではありません。
NotSupportedException elementType はサポートされていません。
ArgumentOutOfRangeException length が 0 未満です。

解説

ほとんどのクラスとは異なり、 Array は、遅延バインディングによるアクセスを可能にするために、パブリック コンストラクタではなく CreateInstance メソッドを用意しています。

参照型の要素は、 null 参照 (Visual Basic では Nothing) に初期化されます。値型の要素は 0 に初期化されます。

使用例

1 次元 Array を作成および初期化する方法を次のコード例に示します。

 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a one-dimensional Array of type Int32.
        Dim my1DArray As Array = Array.CreateInstance(GetType(Int32), 5)
        Dim i As Integer
        For i = my1DArray.GetLowerBound(0) To my1DArray.GetUpperBound(0)
            my1DArray.SetValue(i + 1, i)
        Next i 
        ' Displays the values of the Array.
        Console.WriteLine("The one-dimensional Array contains the " _
           + "following values:")
        PrintValues(my1DArray)
        
    End Sub
    
    Public Shared Sub PrintValues(myArr As Array)
        Dim myEnumerator As System.Collections.IEnumerator = _
           myArr.GetEnumerator()
        Dim i As Integer = 0
        Dim cols As Integer = myArr.GetLength((myArr.Rank - 1))
        While myEnumerator.MoveNext()
            If i < cols Then
                i += 1
            Else
                Console.WriteLine()
                i = 1
            End If
            Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The one-dimensional Array contains the following values:
'     1    2    3    4    5 

[C#] 
using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a one-dimensional Array of type Int32.
      Array my1DArray=Array.CreateInstance( typeof(Int32), 5 );
      for ( int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++ )
         my1DArray.SetValue( i+1, i );

      // Displays the values of the Array.
      Console.WriteLine( "The one-dimensional Array contains the following values:" );
      PrintValues( my1DArray );
   }


   public static void PrintValues( Array myArr )  {
      System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
      int i = 0;
      int cols = myArr.GetLength( myArr.Rank - 1 );
      while ( myEnumerator.MoveNext() )  {
         if ( i < cols )  {
            i++;
         } else  {
            Console.WriteLine();
            i = 1;
         }
         Console.Write( "\t{0}", myEnumerator.Current );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

The one-dimensional Array contains the following values:
    1    2    3    4    5
*/

[C++] 
#using <mscorlib.dll>
using namespace System;

void PrintValues( Array* myArr );

void main()  {
 
    // Creates and initializes a one-dimensional Array instance of type Int32.
    Array* my1DArray = Array::CreateInstance( __typeof(Int32), 5 );

    for ( int i = my1DArray->GetLowerBound(0); i <= my1DArray->GetUpperBound(0); i++ )
        my1DArray->SetValue( __box(i + 1), i );
 
    // Displays the values of the Array.
    Console::WriteLine( "The one-dimensional Array instance contains the following values:" );
    PrintValues( my1DArray );
}
 
 
void PrintValues( Array* myArr )  {
    System::Collections::IEnumerator* myEnumerator = myArr->GetEnumerator();
    int i = 0;
    int cols = myArr->GetLength( myArr->Rank - 1 );
    while ( myEnumerator->MoveNext() )  {
        if ( i < cols ) {
            i++;
        } 
        else {
            Console::WriteLine();
            i = 1;
        }
        Console::Write( "\t{0}", myEnumerator->Current );
    }
    Console::WriteLine();
}

 /*
 This code produces the following output.
 
 The one-dimensional Array instance contains the following values:
     1    2    3    4    5
 */

[JScript] 
import System;

// Creates and initializes a one-dimensional Array of type Int32.
var my1DArray : System.Array =System.Array.CreateInstance( Int32, 5 );
for ( var i : int = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++ )
  my1DArray.SetValue( Int32(i+1), i );

// Displays the values of the Array.
Console.WriteLine( "The one-dimensional Array contains the following values:" );
PrintValues( my1DArray );

 
function PrintValues( myArr : System.Array)  {
   var myEnumerator : System.Collections.IEnumerator = myArr.GetEnumerator();
   var i : int = 0;
   var cols : int = myArr.GetLength( myArr.Rank - 1 );
   while ( myEnumerator.MoveNext() )  {
      if ( i < cols )  {
         i++;
      } else  {
         Console.WriteLine();
         i = 1;
      }
      Console.Write( "\t{0}", myEnumerator.Current );
   }
   Console.WriteLine();
}
 /*
 This code produces the following output.
 
 The one-dimensional Array contains the following values:
     1    2    3    4    5
 */

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Array クラス | Array メンバ | System 名前空間 | Array.CreateInstance オーバーロードの一覧