Type.IsArray 屬性

定義

取得值,以表示類型是否為陣列。

public:
 virtual property bool IsArray { bool get(); };
public:
 property bool IsArray { bool get(); };
public virtual bool IsArray { get; }
public bool IsArray { get; }
member this.IsArray : bool
Public Overridable ReadOnly Property IsArray As Boolean
Public ReadOnly Property IsArray As Boolean

屬性值

如果目前的類型是陣列則為true ,否則為 false

實作

範例

下列範例示範如何使用 IsArray 屬性。

using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      Type[] types = { typeof(String), typeof(int[]),
                       typeof(ArrayList), typeof(Array),
                       typeof(List<String>),
                       typeof(IEnumerable<Char>) };
      foreach (var t in types)
         Console.WriteLine("{0,-15} IsArray = {1}", t.Name + ":",
                           t.IsArray);
   }
}
// The example displays the following output:
//       String:         IsArray = False
//       Int32[]:        IsArray = True
//       ArrayList:      IsArray = False
//       Array:          IsArray = False
//       List`1:         IsArray = False
//       IEnumerable`1:  IsArray = False
open System
open System.Collections

let types = 
    [ typeof<String>; typeof<int[]>
      typeof<ArrayList>; typeof<Array>
      typeof<ResizeArray<string>>
      typeof<seq<char>> ]
for t in types do
    printfn $"""{t.Name + ":",-15} IsArray = {t.IsArray}"""
// The example displays the following output:
//       String:         IsArray = False
//       Int32[]:        IsArray = True
//       ArrayList:      IsArray = False
//       Array:          IsArray = False
//       List`1:         IsArray = False
//       IEnumerable`1:  IsArray = False
Imports System.Collections
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim types() As Type = { GetType(String), GetType(Integer()),
                              GetType(ArrayList), GetType(Array),
                              GetType(List(Of String)),
                              GetType(IEnumerable(Of Char)) }
      For Each t In types
         Console.WriteLine("{0,-15} IsArray = {1}", t.Name + ":", t.IsArray)
      Next
   End Sub
End Module
' The example displays the following output:
'       String:         IsArray = False
'       Int32[]:        IsArray = True
'       ArrayList:      IsArray = False
'       Array:          IsArray = False
'       List`1:         IsArray = False
'       IEnumerable`1:  IsArray = False

備註

屬性 IsArrayfalse 傳回 類別 Array 。 如果目前的實例是 Type 物件,表示集合類型或設計來使用集合的介面,例如 IEnumerableIEnumerable<T> ,它也會傳回 false

若要檢查陣列,請使用下列程式碼:

typeof(Array).IsAssignableFrom(type)
GetType(Array).IsAssignableFrom(type)

如果目前的型別代表泛型型別,或泛型型別或泛型方法定義中的型別參數,這個屬性一律會傳 false 回 。

這是唯讀的屬性。

適用於

另請參閱