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 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 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);
}
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欄位時,實值型別會進行 Boxed。

這個屬性會 true 針對列舉傳回 ,但不會針對 Enum 型別本身傳回 。 如需示範此行為的範例,請參閱 IsEnum

這是唯讀的屬性。

適用於

另請參閱