Latest web development tutorials

C # data types

In C #, variables are divided into the following types:

  • Value Type (Value types)
  • Reference type (Reference types)
  • Pointer type (Pointer types)

Value Type (Value types)

Value type variables can be assigned a value. They are derived from the classSystem.ValueType.

Value types directly contain data. Such asint, char, float, they are stored numbers, letters, floating-point number.When you declare anint type, the system allocates memory to store the value.

The following table lists the values ​​available Type C # 2010:

bool布尔值True 或 FalseFalse
byte8 位无符号整数0 到 2550
char16 位 Unicode 字符U +0000 到 U +ffff'\0'
decimal128 位精确的十进制值,28-29 有效位数(-7.9 x 10 28 到 7.9 x 10 28 ) / 10 0 到 28 0.0M
double64 位双精度浮点型(+/-)5.0 x 10 -324 到 (+/-)1.7 x 10 308 0.0D
float32 位单精度浮点型-3.4 x 10 38 到 + 3.4 x 10 38 0.0F
int32 位有符号整数类型-2,147,483,648 到 2,147,483,6470
long64 位有符号整数类型-923,372,036,854,775,808 到 9,223,372,036,854,775,807 0L
sbyte8 位有符号整数类型-128 到 1270
short16 位有符号整数类型-32,768 到 32,7670
uint32 位无符号整数类型0 到 4,294,967,2950
ulong64 位无符号整数类型0 到 18,446,744,073,709,551,6150
ushort16 位无符号整数类型0 到 65,5350

To get an exact type or a variable size on a particular platform, you can usesizeof method.Expressionsizeof (type)produce storage size in bytes of storage object or type. Here's an example to obtain any type of storage on the machineintSize:

namespace DataTypeApplication
   class Program
      static void Main (string [] args)
         Console.WriteLine ( "Size of int: {0}", sizeof (int));
         Console.ReadLine ();

When the above code is compiled and executed, it produces the following results:

Size of int: 4

Reference type (Reference types)

Reference type does not contain the actual data is stored in a variable, but they contain references to variables.

In other words, they refer to a memory location. When using multiple variables, reference type can point to a memory location. If the data memory location is determined by a change of variable, other variables will automatically reflect this change in values.Built-in reference types:object, dynamic andstring.

Object (Object) type

Object (Object) type is ageneric C # type system (Common Type System - CTS) in the ultimate base class for all data types. Object is an alias for System.Object class. So the object (Object) type can be assigned to any other type (value types, reference types, pre-defined types or user-defined type) values. However, before you assign a value, you need to type conversion.

When a value type to object type is calledboxing; on the other hand, when an object type to a value type, it is called unboxing.

object obj;
obj = 100; // this is boxing 

Dynamic (Dynamic) Type

You can store any type of value in the dynamic data type of the variable. These variables are type checking occurs at run time.

Dynamic type declaration syntax:

dynamic <variable_name> = value;


dynamic d = 20;

Similar dynamic type of an object type, but the type of inspection object type variable occurs at compile time, dynamic type checking and type of the variable occurs at runtime.

String (String) type

String (String) type allows you toassign any string value to a variable. String (String) type is an alias for System.String class. It is from the object (Object) type derived. Values ​​String (String) type can be distributed in two forms: @ quotes and quotes.


String str = "";

@ A quoted string:

@ "";

C # string in front of the string can be added @ (referred to as "verbatim string") will escape character (\) be treated as ordinary characters, such as:

string str = @"C:\Windows";

Equivalent to:

string str = "C:\\Windows";

@ String can be used in any row, line breaks and indentation spaces are counted in the length of the string.

string str = @"<script type=""text/javascript"">

User-defined reference types: class, interface or delegate. We will discuss these types in later chapters.

Pointer type (Pointer types)

Pointer type variable to store another type of memory addresses. C # and pointers in C or C ++ pointers have the same functionality.

Pointer type declaration syntax:

type * identifier;


char * cptr;
int * iptr;

We will discuss the type of the pointer in the section "Unsafe Code".