How to distinguish that a type is ValueType Or RefereceType?
-
19-09-2019 - |
Question
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?
Solution
All structs, enums and native types are value types.
At runtime you can check like this:
Type type = typeof(TypeName);
if (type.IsValueType)
{
//...
}
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow