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

注解

属性IsArrayArray 类返回false。 如果当前实例是表示Type集合类型或设计用于处理集合的接口(如 或 IEnumerable<T>IEnumerable的对象,则它还返回 false

若要检查数组,请使用如下代码:

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

如果当前类型表示泛型类型或泛型类型或泛型方法的定义中的类型参数,则此属性始终返回 false

此属性为只读。

适用于

另请参阅