用.net静态变量取代Application,速度更快

来源:互联网 发布:遗传算法的数学基础 编辑:程序博客网 时间:2024/05/19 10:09
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
在传统的ASP中,我们要用application对象去存储应用于整个application的变量。这当然会带来内存消耗的代价。在.net中,我们可以用static变量来改善它,采用static 变量在大多数时候存储的速度会比application对象快。

做法:

创建一个webApplication,假设名称为webApplication1,在Global.aspx中的Global类中增加一个静态的成员sGreeting.

public class Global : System.Web.HttpApplication

{

public static string sGreeting = "China is great!";

……

}

在WebForm1中增加一个label,名称为label1.

在page_load()中为label1的text属性赋值。

private void Page_Load(object sender, System.EventArgs e)
{

Label1.Text = WebApplication1.Global.sGreeting;
}

运行程序后将会看到China is great!

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击