C# 中,变量分为以下几种类型:
- 值类型(Value types)
- 引用类型(Reference types)
- 指针类型(Pointer types)
值类型(Value types)
值类型直接包含数据。
比如 int、char、float,它们分别存储数字、字符、浮点数
sizeof 方法。表达式 sizeof(type) 产生以字节为单位存储对象或类型的存储尺寸
using System;
namespace DataTypeApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of int: {0}", sizeof(int));
Console.ReadLine();
}
}
}
Size of int: 4
引用类型(Reference types)
内置的 引用类型有:object、dynamic 和 string。
当一个值类型转换为对象类型时,则被称为 装箱;另一方面,当一个对象类型转换为值类型时,则被称为 拆箱
object obj;
obj = 100; // 这是装箱
动态(Dynamic)类型
dynamic d = 20;
动态类型与对象类型相似,但是对象类型变量的类型检查是在编译时发生的,而动态类型变量的类型检查是在运行时发生的。
字符串(String)类型
string str = @"C:\Windows";
//等价与
string str = "C:\\Windows";
@(称作"逐字字符串")将转义字符(\)当作普通字符对待
指针类型(Pointer types)
char* cptr;
int* iptr;