C# Data types

In C#, variables can be divided into the following types:

  • Value types
  • Reference types
  • Pointer types

Value types

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

is changed to "d", the value stored in the memory space 0X55 in the "heap" is modified. Because the address of a2 is the same as the address of a1, the output result is a b d.int、char、float, and they store numbers, characters, and floating-point numbers respectively. When you declare ainttype, the system allocates memory to store the value.

The following table lists the value types available in C# 2010:

TypeDescriptionScopeDefault value
boolBoolean valueTrue or FalseFalse
byte8-bit unsigned integer0 to 2550
char16-bit Unicode charactersU+0000 to U+ffff'\0'
decimal128-bit precise decimal values, with 28-29 significant digits(-7.9 x 1028to 7.9 x 1028) / 100 to 28 0.0M
double64-bit double-precision floating-point type(+/-)5.0 x 10-324to (+/-)1.7 x 103080.0D
float32-bit single-precision floating-point type-3.4 x 1038to + 3.4 x 10380.0F
int32-bit signed integer type-2,147,483,648 to 2,147,483,6470
long64-bit signed integer type-9,223,372,036,854,775,808 to 9,223,372,036,854,775,8070L
sbyte8-bit signed integer type-128 to 1270
short16-bit signed integer type-32,768 to 32,7670
uint32-bit unsigned integer type0 to 4,294,967,2950
ulong64-bit unsigned integer type0 to 18,446,744,073,709,551,6150
ushort16-bit unsigned integer type0 to 65,5350

To obtain the exact size of a type or a variable on a specific platform, you can usesizeofMethods. Expressionssizeof(type)produces the storage size of an object or type in bytes. The following example obtains [the size] on any machineintStorage size of types:

Example

using System;

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 types

Reference types do not contain the actual data stored in the variable, but they contain references to the variable.

In other words, they refer to a memory location. When multiple variables are used, reference types can point to a memory location. If the data at the memory location is changed by one variable, other variables will automatically reflect this change in value.built-inReference types include:object、dynamicandstring。

Object type

Object Typeis the ultimate base class of all data types in the C# Common Type System (CTS). Object is an alias for the System.Object class. Therefore, the object (Object) type can be assigned values of any other type (value types, reference types, predefined types, or user-defined types). However, before assigning a value, type conversion is required.

When a value type is converted to an object type, it is calledBoxing; on the other hand, when an object type is converted to a value type, it is calledUnboxing。

object obj;
obj = 100; // 这是装箱

Dynamic type

You can store any type of value in a dynamic data type variable. Type checking for these variables occurs at runtime.

Syntax for declaring a dynamic type:

dynamic <variable_name> = value;

For example:

dynamic d = 20;

Dynamic types are similar to object types, but type checking for object type variables occurs at compile time, while type checking for dynamic type variables occurs at runtime.

String type

String typeallows you to assign any string value to a variable. The string (String) type is an alias for the System.String class. It is derived from the object (Object) type. Values of the string (String) type can be assigned in two forms: quotation marks and @ quotation marks.

For example:

String str = "example.com";

A @-quoted string:

@"example.com";

In C#, a string can be prefixed with @ (called a "verbatim string") to treat escape characters (\) as ordinary characters, for example:

string str = @"C:\Windows";

Equivalent to:

string str = "C:\\Windows";

In an @ string, you can break lines arbitrarily; line breaks and indentation spaces are all counted within the string length.

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

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

Pointer types

Pointer type variables store the memory address of another type. Pointers in C# have the same functionality as pointers in C or C++.

Syntax for declaring a pointer type:

type* identifier;

For example:

char* cptr;
int* iptr;

We will discuss pointer types in the chapter "Unsafe Code."

other extensions