Type.IsValueType 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會得到一個值來表示 是否 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。
這個屬性是唯讀的。