什么是数据类型(Data Type)
A data type is a homogeneous collection of values, effectively presented, equiipped with a set of operations which manipulate these values
C#类型中所包含的信息
- 储存此类型变量所需要的内存空间
- 此类型的值可表示的最大,最小值的范围
- 此类型所包含的成员(如方法,属性,事件等)
- 此类型由何基类派生而来
- 程序运行的时候,此类型的变量应该分配在内存的什么位置
程序运行之后,在内存中包括两个部分:
a)栈–Stack 给函数调用使用 Stack Overflow栈溢出(无限递归,手动从栈里切内存过多都会导致)
b)堆–Heap
内存泄漏(声明的对象,使用完毕后,没有回收,导致资源浪费)- 此类型所允许的操作
C#的五大数据类型
引用类型
- 类(class):如Windows,Form ----------------class
- 接口--------------------------------------------interface
- 委托--------------------------------------------delegate
值类型
- 结构(Structures):如Int32,Int64 ---------struct
- 枚举--------------------------------------enum
所有类型的基类object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace StudyTest
{
public class DataType
{
public static void ShowInt32()
{
Type type = typeof(int);
Console.WriteLine($"int类型的类名为:{type.Name}");
Console.WriteLine($"int类型的父类的类名为:{type.BaseType.Name}");
//获取int类型的方法签名
MethodInfo[] intMethods= type.GetMethods();
Console.WriteLine($"方法名称\t访问修饰符\t返回值类型\t参数列表");
foreach (MethodInfo method in intMethods)
{
StringBuilder stringBuilder = new StringBuilder();
//输出方法名
stringBuilder.Append(method.Name+ "\t");
//输出方法的访问修饰符
stringBuilder.Append((method.IsPublic?"public":method.IsPrivate?"private":"")+"\t");
//输出方法的返回值类型
stringBuilder.Append(method.ReturnType.Name == null ? "void" : method.ReturnType.Name+"\t");
//获取每个方法的参数列表
ParameterInfo[] parameterInfos = method.GetParameters();
foreach (ParameterInfo p in parameterInfos)
{
//获取每个参数的类型和名称
stringBuilder.Append($"({p.GetType().Name} {p.Name})");
}
//输出结果
Console.WriteLine(stringBuilder.ToString());
}
}
}
}