Type.IsValueType 属性

定义

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

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

属性值

true 如果为值类型,则为 Type ;否则为 false

实现

示例

以下示例创建一个类型 MyEnum变量,检查 IsValueType 属性并显示结果。

using System;

// Declare an enum type.
enum NumEnum { One, Two }

public class Example
{

    public static void Main(string []args)
    {
        bool flag = false;
        NumEnum testEnum = NumEnum.One;
        // Get the type of testEnum.
        Type t = testEnum.GetType();
        // Get the IsValueType property of the testEnum variable.
        flag = t.IsValueType;
        Console.WriteLine("{0} is a value type: {1}", t.FullName, flag);
    }
}
// The example displays the following output:
//        NumEnum is a value type: True
// Declare an enum type.
type NumEnum = One = 1 | Two = 2

let testEnum = NumEnum.One
// Get the type of testEnum.
let t = testEnum.GetType()
// Get the IsValueType property of the testEnum variable.
let flag = t.IsValueType
printfn $"{t.FullName} is a value type: {flag}"
// The example displays the following output:
//        NumEnum is a value type: True
' Declare an enum type.
Enum NumEnum
    One
    Two
End Enum
    
Public Class Example

    Public Shared Sub Main()
        Dim flag As Boolean = False
        Dim testEnum As NumEnum = NumEnum.One
        ' Get the type of myTestEnum.
        Dim t As Type = testEnum.GetType()
        ' Get the IsValueType property of the myTestEnum variable.
         flag = t.IsValueType()
         Console.WriteLine("{0} is a value type: {1}", t.FullName, flag)
     End Sub 
 End Class  
' The example displays the following output:
'       NumEnum is a value type: True

注解

值类型是表示为位序列的类型;值类型不是类或接口。 在某些编程语言中,值类型称为“结构”。 枚举是值类型的特殊情况。

此属性 false 返回类 ValueType ,因为 ValueType 不是值类型本身。 它是所有值类型的基类,因此可以为其分配任何值类型。 如果 ValueType 自身是值类型,则无法执行此操作。 将值类型分配给类型的 ValueType字段时进行装箱。

此属性 true 返回枚举,但不返回类型 Enum 本身。 有关演示此行为的示例,请参阅 IsEnum

此属性为只读。

适用于

另请参阅