views:

134

answers:

2

Hi 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?

+1  A: 

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.

Martinho Fernandes
Reading you comment, DateTime is a Value Type. So what does it mean?DateTime dt = new DateTimeThank you
odiseh
I think you are confusing value/reference types with whether or not certain types have literals. Strings have string literals that can be used to initialize them, but they are not value types. DateTime does not have any literals, but is a value type. What that means is that when you pass a DateTime as a parameter to a function, or write DateTime datetime1 = datetime2, the value is actually copied instead of both datetime's referring to the same object.
Tal Pressman
What Tal said is totally right. The choice of what gets or doesn't get literals is totally up to the language. What is and what is not a value type is defined by the CLI itself. For instance, VB has DateTime literals. One can also have list or hash literals (boo, IronPython, IronRuby), and those are still reference types.
Martinho Fernandes
+3  A: 

All structs, enums and native types are value types.

At runtime you can check like this:

Type type = typeof(TypeName);

if (type.IsValueType) 
{ 
   //...
}
Philippe Leybaert
Only in the .net world.
David Schmitt
Very good point :-)
Philippe Leybaert