C# Static Variable 與 Application

C# Static Variable 與 Application

網路上已經有很多關於 C# Static 的文章,就不多述了
但 Static 是不是跟 Application有一樣的效果??
於是進行了測試,從測試結果中可以發現
即使從 不同Client 端的瀏覽器Static變數仍然是累加上去的


static_variable.cs

public class static_variable
{
   public static int count;	// static
   public int number;		   // nostatic
   
   public static_variable()
   {			
      number = 0;
      number++;
      count++;
   }
   
   public void showData()
   {
      System.Web.HttpContext.Current.Response.Write("-----------------<br>");
      System.Web.HttpContext.Current.Response.Write(string.Format("count 》{0}<br>",count));
      System.Web.HttpContext.Current.Response.Write(string.Format("number 》{0}<br>",number));

      int count_d = 0;
      count_d--;			
      System.Web.HttpContext.Current.Response.Write(string.Format("count_d 》{0}<br>",count_d));
   }		
}

webform.aspx

private void Page_Load(object sender, System.EventArgs e)
{
   // 在這裡放置使用者程式碼以初始化網頁

   // static_variable.count = 0;
      
   static_variable a = new static_variable();
   a.showData();
      
   static_variable b = new static_variable();			
   b.showData();
      
   static_variable c = new static_variable();
   c.showData();			

}

2015-11-13_190601


2015-11-13_190609


2015-11-13_190618