當在派生類中重寫時,使用Type.IsArrayImpl()方法,實現IsArray屬性並確定Type是否為數組。
用法: protected abstract bool IsArrayImpl ();
返回值:如果Type是數組,則此方法返回true,否則返回false。
以下示例程序旨在說明Type.IsArrayImpl()方法的使用:
示例1:
// C# program to demonstrate the
// Type.IsArrayImpl() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// creating and initializing object of MyClass
MyClass mytype = new MyClass(typeof(int));
// checking if mytype has any
// elementtype or not
if (mytype.HasElementType)
Console.WriteLine("The type of myArray is {0}.",
mytype.elementtype);
else
Console.WriteLine("myArray is not an array, pointer,"+
"or reference type.");
}
}
// Defining MyClass extended from TypeDelegator
public class MyClass : TypeDelegator {
// creating and initializing
// elementtype with null
public string elementtype = null;
// creating and initializing
// type with null
private Type type = null;
// Constructor
public MyClass(Type type)
: base(type)
{
this.type = type;
}
// Override Type.IsArrayImpl().
protected override bool IsArrayImpl()
{
// Determine whether the type is an array.
if (type.IsArray)
{
elementtype = "array";
return true;
}
// Return false if the type is not
// a reference, array, or pointer type.
return false;
}
}
輸出:
myArray is not an array, pointer,or reference type.
示例2:
// C# program to demonstrate the
// Type.IsArrayImpl() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// creating and initializing object of MyClass
MyClass mytype = new MyClass(typeof(int[,,,, ]));
// checking if mytype has
// any elementtype or not
if (mytype.HasElementType)
Console.WriteLine("The type of {0} is array.",
mytype.type);
else
Console.WriteLine("myArray is not an array,"+
"pointer, or reference type.");
}
}
// Defining MyClass extended from TypeDelegator
public class MyClass : TypeDelegator {
// creating and initializing
// elementtype with null
public string elementtype = null;
// creating and initializing
// type with null
public Type type = null;
// Constructor
public MyClass(Type type)
: base(type)
{
this.type = type;
}
// Override Type.IsArrayImpl().
protected override bool IsArrayImpl()
{
// Determine whether the
// type is an array.
if (type.IsArray)
{
elementtype = "array";
return true;
}
// Return false if the type is not
// a reference, array, or pointer type.
return false;
}
}
輸出:
The type of System.Int32[,,,,] is array.
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.IsArrayImpl() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。