再次讨论关于C#中的static问题!

tuoji 2004-04-30 09:49:59
1、ASP.NET中的static生命周期到底是在何时结束?
好像不是在页面运行完毕结束。因为我测试了一个static变量,每次刷新页面,static变量的值都是以前的,说明这个static变量的值没有丢失。那static变量的生命周期到底何时结束?

2、ASP.NET中的static是否所有用户共享?
也就是对于一个static变量,是否访问这个页面的所有用户得到的这个static变量的值都一样?我自己没法测试,所以也不知道。知道的告诉一下。谢谢!
...全文
246 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mikespook 2004-04-30
  • 打赏
  • 举报
回复
DOTNET有一个和手工资源回收不同的地方就是资源回收的不确定性~~~~
using System;

namespace DemoClass
{
/// <summary>
/// 第一章练习
/// </summary>
public class DemoClass
{
public static long Instances;
public DemoClass()
{
Instances++;
}
~DemoClass()
{
Instances--;
}
}
}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DemoClass;

namespace DemoTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(344, 16);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(288, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 1;
this.button1.Text = "生成";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(360, 53);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
int Counter;
DemoClass.DemoClass dc;
for(Counter = 0; Counter < 1000; Counter++)
{
dc = new DemoClass.DemoClass();
}
}

private void timer1_Tick(object sender, System.EventArgs e)
{
label1.Text = DemoClass.DemoClass.Instances.ToString();
}
}
}
看这个例子~~~你就明白了~~~~~
xierimin 2004-04-30
  • 打赏
  • 举报
回复
static的值对一个页而言,也是唯一的.
在C#中,Codebind模式下,是从Page类继承,那么调用一个类的static成员.也是唯一的.不会改变.
dayasky 2004-04-30
  • 打赏
  • 举报
回复



一个类的static元素是唯一的,该类的所有对象共用相同的static成员
页面也是一个类,当然是所有用记的实例页面共用相同的static成员
chnama 2004-04-30
  • 打赏
  • 举报
回复
关注

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧