Type.IsValueType 属性

定义

获取一个值,通过该值指示 Type 是否为值类型。Gets a value indicating whether the Type is a value type.

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

属性值

如果 true 是值类型,则为 Type;否则为 falsetrue if the Type is a value type; otherwise, false.

实现

示例

下面的示例创建一个 MyEnum类型的变量,检查 IsValueType 属性并显示结果。The following example creates a variable of type MyEnum, checks for the IsValueType property, and displays the result.

using namespace System;

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

int main()
{
    bool flag = false;
    NumEnum testEnum = NumEnum::One;
      
    // Get the type of myTestEnum.
    Type^ t = testEnum.GetType();
      
    // Get the IsValueType property of the myTestEnum
    // variable.
    flag = t->IsValueType;
    Console::WriteLine("{0} is a value type: {1}", t->FullName, flag);
}
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 myTestEnum.
        Type t = testEnum.GetType();
        // Get the IsValueType property of the myTestEnum 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.
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

注解

值类型是表示为位序列的类型;值类型不是类或接口。Value types are types that are represented as sequences of bits; value types are not classes or interfaces. 在某些编程语言中,值类型称为 "结构"。Value types are referred to as "structs" in some programming languages. 枚举是值类型的一种特殊情况。Enums are a special case of value types.

此属性将返回 ValueTypefalse,因为 ValueType 不是值类型本身。This property returns false for the ValueType class, because ValueType is not a value type itself. 它是所有值类型的基类,因此可以为其分配任何值类型。It is the base class for all value types, and therefore any value type can be assigned to it. 如果 ValueType 本身是值类型,则无法进行此操作。This would not be possible if ValueType itself was a value type. 如果将值类型分配给类型 ValueType的字段,则会将这些值类型装箱。Value types are boxed when they are assigned to a field of type ValueType.

此属性返回枚举的 true,但不返回 Enum 类型本身的。This property returns true for enumerations, but not for the Enum type itself. 有关演示此行为的示例,请参阅 IsEnumFor an example that demonstrates this behavior, see IsEnum.

此属性是只读的。This property is read-only.

适用于

另请参阅