odiseh
odiseh

Reputation: 26547

How to distinguish that a type is ValueType Or RefereceType?

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?

Upvotes: 5

Views: 1523

Answers (2)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234644

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.

Upvotes: 4

Philippe Leybaert
Philippe Leybaert

Reputation: 171864

All structs, enums and native types are value types.

At runtime you can check like this:

Type type = typeof(TypeName);

if (type.IsValueType) 
{ 
   //...
}

Upvotes: 7

Related Questions