C#变量的生存周期问题

来源:百度知道 编辑:UC知道 时间:2024/05/05 05:30:06
namespace c1
{
public class stack是
{
private int []arr;
private int sp; //栈顶
private int count;
public stack(int length)
{
count=length;
sp=0;
arr=new int[length];

}
public stack()
{
count=10;
sp=0;
arr=new int[10];

}
public void push(int element) //压栈
{
if(sp>=count)
MessageBox.Show("已超出栈顶,压栈失败","栈已满");
else
{
arr[sp]=element;

sp=sp+1;
}

}

public int pop() //出栈
{
if(sp<=0)
{
MessageBox.Show("栈为空,出栈失败");
return(0);
}
else
{
sp=sp-1;
return(arr[sp]);
}
}
}
}

Button1的Click事件代码

c#生存周期问题和c++一样
比如sp
从构造函数开始产生,到析构函数时消亡

但是c#由于采用托管,所以不能定义明确析构函数
系统会根据资源情况自动决定何时析构