文章目录
一、类型
1.类型在C#语言中的作用
①查看类型由何基类派生而来
namespace TypeSample
{
class Program
{
static void Main(string[] args)
{
//Form类的基类(父类)是System.Windows.Forms.ContainerControl
Type pType = typeof(Form);
Console.WriteLine(pType.BaseType.FullName);
}
}
}
②查看类型所包含的成员(如属性、方法、事件等)
namespace TypeSample
{
class Program
{
static void Main(string[] args)
{
//查看Form类的成员
//查看Form类所有的属性
Type pType1 = typeof(Form);
PropertyInfo[] pInfos = pType1.GetProperties(); //using System.Reflection;
foreach (var p in pInfos)
{
Console.WriteLine(p.Name);
}
//查看Form类的成员
//查看Form类所有的方法
Type pType2 = typeof(Form);
MethodInfo[] pInfos1 = pType2.GetMethods();
foreach (var m in pInfos1)
{
Console.WriteLine(m.Name);
}
}
}
}
③查看类型所允许的操作(运算)
namespace TypeSample
{
class Program
{
static void Main(string[] args)
{
//数据类型知道自己所允许的操作是哪些
//双精度型
double result = 3.0 / 4.0; //结果:0.75
Console.WriteLine(result);
//整数型
double result1 = 3 / 4; //结果:0
Console.WriteLine(result1);
}
}
}
2.堆栈的简单理解
堆:
namespace HeapSample
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 消耗内存
/// </summary>
List<Window> winList; //声明winList变量
private void butto1_Click(object sender, RoutedEventArgs e)
{
winList = new List<Window>();
for (int i = 0; i < 15000; i++)
{
Window w = new Window();
winList.Add(w);
}
}
/// <summary>
/// 释放内存
/// </summary>
private void button2_Click(object sender, RoutedEventArgs e)
{
winList.Clear();
}
}
}
栈:
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
//栈—函数调用;体积小、速度快
//Stack Overflow ——函数调用过多(算法错误);在栈上分配了过多内存
BadGuy bg = new BadGuy();
bg.BadMethod();
}
}
class BadGuy
{
public void BadMethod()
{
//不良的递归
int x = 100;
this.BadMethod(); //一直在调用函数本身,递归
}
}
}
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
//栈—函数调用;体积小、速度快
//爆栈;在栈上分配过大内存:
unsafe
{
int* p = stackalloc int[9999999];
}
}
}
}
3.C#的类型系统
C#的五大数据类型:
类(Classes):如Window,Form,Console,String
结构体(Structures):如Int32,Int64,Single,Double
枚举(Enumerations):如HorizontalAlignment,Visibility
接口(Interfaces)
委托(Delegates)
二、变量
1.变量定义
变量表示了存储位置,并且每个变量都有一个类型,以决定什么样的值能够存入变量。
- 变量名表示(对应着)变量的值在内存中的存储位置
- 计算机系统通过变量的类型来分配给它对应大小的内存空间
变量(广义)一共有7种:
静态变量,实例变量(成员变量,字段),数组元素,值参数,引用参数,输出形参,局部变量
狭义的变量指局部变量,因为其它种类的变量都有自己的约定名称。
局部变量就是方法体(函数体)里声明的变量
2.变量举例解释
①静态变量(静态成员变量)
②字段(非静态成员变量)
字段是属性的雏形,可以赋任意值。
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
//字段是属性的雏形,可以赋任意值
Student stu = new Student();
stu.Age = -1; //人的年龄不可能为-1
}
}
class Student
{
public int Age;
}
}
③数组元素
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[100]; //声明长度为100的整型数组
array[0] = 100; //array[0]—>array[99] 100个整数都是变量
}
}
}
④值参数,引用参数,输出形参
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
double x = stu.Add(2, 3);
Console.WriteLine(x);
}
}
class Student
{
public double Add(double a,double b) //函数参数a,b—值参数变量
{
return a + b;
}
}
}
⑤局部变量
局部变量就是方法体(函数体)里声明的变量
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
int x; //在Main函数体里面声明了变量x —> 局部变量x
}
}
}
3.变量的声明
class Student
{
// pulic static 有效的修饰符组合(可选)
// int 变量类型
// Amount 变量名-合法的标识符(驼峰命名法)
// = 0 初始化器(可选)
public static int Amount = 0;
}
三、内存
1.值类型的变量在内存中的存储方式
值类型:结构体、枚举
- 以byte、sbyte、short、ushort为例
- 值类型没有实例,所谓的"实例"与变量合二为一
byte类型的变量在内存中存储:
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
byte b; //byte类型的变量b;一个字节
b = 100; //十进制100 转为 二进制 01100100
}
}
}
sbyte类型的变量在内存中存储:
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
sbyte sb; //sbyte类型的变量sb;一个字节
sb = 100; //十进制100 转为 二进制
}
}
}
2.引用类型的变量在内存中的存储方式
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
//变量stu的类型是Student类—Student类是引用类型
//计算机看到引用类型,直接给它分配 4 个字节,而且全部 bit 置 0,告诉你这个变量没有引用任何实例
Student stu;
//在堆内存里创建一个Student实例;占了6个字节
stu = new Student();
//把stu里的值copy到stu2
Student stu2;
stu2 = stu;
}
}
/// <summary>
/// 引用类型—类
/// </summary>
class Student
{
//ID,Store:结构体类型的变量—值类型
//值类型是按这种类型的实际大小分配内存
uint ID; //uint:4个字节
ushort Store; //ushort:2个字节
}
}
①计算机看到引用类型,直接给它分配 4 个字节,而且全部 bit 置 0,告诉你这个变量没有引用任何实例
//变量stu的类型是Student类—Student类是引用类型
//计算机看到引用类型,直接给它分配 4 个字节,而且全部 bit 置 0,告诉你这个变量没有引用任何实例
Student stu;
②在堆内存里创建一个Student实例
现在,引用变量stu里面存的是实例new Student()的地址。
在堆内存里,存uint类型的ID字段占4个字节;存ushort类型的Store字段占2个字节;共6个字节。前四个uint,后两个ushort。
图中左侧是栈,右侧是堆。
//在堆内存里创建一个Student实例;占了6个字节
stu = new Student();
//变量stu的类型是Student类—Student类是引用类型
//计算机看到引用类型,直接给它分配 4 个字节,而且全部 bit 置 0,告诉你这个变量没有引用任何实例
Student stu;
//在堆内存里创建一个Student实例;占了6个字节
stu = new Student();
//把stu里的值copy到stu2
Student stu2;
stu2 = stu;
☆Notes:
(1)局部变量stu,stu2在stack上分配内存
(2)new Student()实例在heap上分配内存
(3)成员变量ID,Store有默认值:
①一旦变量在内存中分配好后,它的内存块就被统统刷成 0,这就是它的默认值。
②局部变量没有默认值,因为 C# 为了避免不安全代码,要求局部变量必需有显式赋值。
```csharp
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student();
Console.WriteLine(stu.ID); //默认值为0
Console.WriteLine(stu.Store); //默认值为0
}
}
class Student
{
//成员变量ID,Store
public uint ID;
public ushort Store;
}
}
四、装箱与拆箱
1.装箱
-
发现object类型的变量obj所要引用的值不是堆上的实例,而是栈上的值类型的值;会先copy栈上的值类型的值,再在堆上面找一块可以存储的空间。
-
把栈上的值类型的值封装成一个object类型的实例放在堆上。
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
int x = 100;
object obj; //引用类型obj
}
}
}
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
int x = 100;
object obj; //引用类型obj
obj = x
}
}
}
2.拆箱
- 把堆上面object类型的实例里面的值,按要求拆成目标数据类型,存储到栈上
namespace TypeInCSharp
{
class Program
{
static void Main(string[] args)
{
int x = 100;
object obj; //引用类型obj
obj = x;
int y = (int)obj; //获取obj在堆内存上所存储的值
Console.WriteLine(y); //结果是100
}
}
}