Type.IsArray 属性

定义

获取一个值,该值指示类型是否为数组。

C#
public virtual bool IsArray { get; }
C#
public bool IsArray { get; }

属性值

如果当前类型是数组,则为 true;否则为 false

实现

示例

下面的示例演示如何使用 IsArray 属性。

C#
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

注解

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

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

C#
typeof(Array).IsAssignableFrom(type)

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

此属性为只读。

适用于

产品 版本
.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, 10
.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅