How to distinguish that a type is ValueType Or RefereceType?

前端 未结 2 956
失恋的感觉
失恋的感觉 2021-01-13 03:13

some simple types like int, string , ....are easy to realize that they are ValueTypes Or RefrenceTypes. But I wanna to know is there any way to distinguish?

相关标签:
2条回答
  • 2021-01-13 03:49

    All structs, enums and native types are value types.

    At runtime you can check like this:

    Type type = typeof(TypeName);
    
    if (type.IsValueType) 
    { 
       //...
    }
    
    0 讨论(0)
  • 2021-01-13 03:49

    Strings are not value types.

    Here is a list of the most commonly used value types:

    • bool (System.Boolean)
    • byte (System.Byte)
    • char (System.Char)
    • decimal (System.Decimal)
    • double (System.Double)
    • float (System.Single)
    • int (System.Int32)
    • long (System.Int64)
    • sbyte (System.SByte)
    • short (System.Int16)
    • uint (System.UInt32)
    • ulong (System.UInt64)
    • ushort (System.UInt16)
    • System.DateTime

    Besides those:

    • Any type that is an enum
    • Any type that is a struct

    All other types are reference types.

    0 讨论(0)
提交回复
热议问题