閱讀英文

共用方式為


Type.IsValueType 屬性

定義

取得值,指出 Type 是否為實值類型。

C#
public bool IsValueType { get; }

屬性值

如果 true 是實值類型,則為 Type,否則為 false

實作

範例

下列範例會建立 類型的 MyEnum變數、檢查 IsValueType 屬性,並顯示結果。

C#
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

備註

實值型別是以位序列表示的類型;實值型別不是類別或介面。 實值型別在某些程式設計語言中稱為「結構」。 列舉是實值型別的特殊案例。

這個屬性會 false 傳回 類別 ValueType ,因為 ValueType 不是實值型別本身。 它是所有實值型別的基類,因此可以將任何實值型別指派給它。 如果 ValueType 本身是實值型別,則不可能這麼做。 當實值型別指派給 類型的 ValueType欄位時,實值型別會進行 Boxed。

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

這是唯讀的屬性。

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱