- C#中所有的数据类型都派生于Object类,根据拷贝策略可分为两类:引用类型(ReferenceType)、值类型(ValueType)。
- 引用类型包括类(class)、接口(interface)、委托(delegate)。
- 值类型包括结构体(struct)、枚举(enum)。
- C#预定义了一组简单类型,通过关键字标识。其中,引用类型包括Object类型(object)、字符串类型(string),值类型包括整数类型(sbyte、byte、short、ushort、int、uint、long、ulong)、二进制浮点类型(float、double)、十进制浮点类型(decimal)、布尔类型(bool)、字符类型(char)。
- 变量表示存储位置,指向以其名称所对应的内存地址为起点、以其表示的数据类型所要求的存储空间为长度的一块内存区域。
- 变量可分为静态变量、实例变量、数组元素(params)、值参数、引用参数(ref)、输出参数(out)、局部变量。
- 引用类型变量指向的内存区域中存储着实例地址,值类型变量指向的内存区域中存储着实例本身。
- 引用类型变量指向的内存区域长度为一个引用的长度,一般等于操作系统的位数,值类型变量指向的内存区域长度为所存储的数据类型的长度。
- 引用类型变量指向堆(Heap)上的内存区域,值类型变量指向栈(Stack)上的内存区域。
- 装箱就是将栈上的值类型转换成堆上的引用类型,拆箱就是将堆上的引用类型转换成栈上的值类型。
- 值参数会指向一块新的内存区域,用于存储引用类型变量中的实例地址或值类型变量中的实例本身,而引用参数和输出参数会直接指向原变量所指向的内存区域。
- 输出参数与引用参数类似,但传给引用参数的变量需要被明确赋值,而传给输出参数的变量不需要被明确赋值。
- 数组参数必须是参数列表中的最后一个参数。
using System;
namespace _04
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*****测试引用类型*****");
GetInheritChain(typeof(ClassType));
GetInheritChain(typeof(InterfaceType));
GetInheritChain(typeof(DelegateType));
Console.WriteLine();
Console.WriteLine("*****测试值类型*****");
GetInheritChain(typeof(StructType));
GetInheritChain(typeof(EnumType));
Console.WriteLine();
Console.WriteLine("*****测试预定义类型*****");
GetInheritChain(typeof(object));
GetInheritChain(typeof(string));
GetInheritChain(typeof(sbyte));
GetInheritChain(typeof(byte));
GetInheritChain(typeof(short));
GetInheritChain(typeof(ushort));
GetInheritChain(typeof(int));
GetInheritChain(typeof(uint));
GetInheritChain(typeof(long));
GetInheritChain(typeof(ulong));
GetInheritChain(typeof(float));
GetInheritChain(typeof(double));
GetInheritChain(typeof(decimal));
GetInheritChain(typeof(bool));
GetInheritChain(typeof(char));
Console.WriteLine();
}
public static void GetInheritChain(Type type)
{
Console.Write(type.FullName);
type = type.BaseType;
while (type != null)
{
Console.Write($" => {type.FullName}");
type = type.BaseType;
}
Console.WriteLine();
}
}
class ClassType
{
}
interface InterfaceType
{
}
delegate void DelegateType();
struct StructType
{
}
enum EnumType
{
}
}