C# 程序检查指定类型是否为指针
指针是包含另一个变量的引用的变量。或者换句话说,指针是一个变量,存储了同类型变量的地址。例如,字符串指针可以存储字符串的地址。在 C# 中,我们可以通过使用 Type 类的 IsPointer 属性来检查给定类型是否为指针。如果指定的类型是指针,则此属性返回 true。否则,它将返回 false。它是一个只读属性。
句法:
public bool IsPointer{ get; }
示例 1:
C#
// C# program to check whether
// the given type is pointer or not
using System;
using System.Reflection;
class GFG{
static void Main()
{
// Create an array with 5 elements of integer type
int[] var1 = new int[5];
string var2 = "GeeksforGeeks";
float var3 = 3.45f;
// Check the type is pointer or not
// Using IsPointer property
Console.WriteLine(var1.GetType().IsPointer);
Console.WriteLine(var2.GetType().IsPointer);
Console.WriteLine(var3.GetType().IsPointer);
}
}
C#
// C# program to check whether
// the given type is pointer or not
using System;
using System.Reflection;
class GFG{
static void Main()
{
// Create an array of integer type with 7 elements
int[] array1 = new int[7];
// Check the type is pointer or not
// Using IsPointer property
if (array1.GetType().IsPointer == true)
{
Console.WriteLine("The given type is a pointer");
}
else
{
Console.WriteLine("The given type is not a pointer");
}
}
}
输出:
False
False
False
示例 2:
C#
// C# program to check whether
// the given type is pointer or not
using System;
using System.Reflection;
class GFG{
static void Main()
{
// Create an array of integer type with 7 elements
int[] array1 = new int[7];
// Check the type is pointer or not
// Using IsPointer property
if (array1.GetType().IsPointer == true)
{
Console.WriteLine("The given type is a pointer");
}
else
{
Console.WriteLine("The given type is not a pointer");
}
}
}
输出:
The given type is not a pointer