global.asax中的静态变量 [英] static variables in global.asax

查看:85
本文介绍了global.asax中的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是一个相当简单的问题。


在ASP.NET应用程序中说,我希望将常见的常量分享为静态的

变量global.asax(我知道有web.config bla bla ..但是我只想说

说我想使用global.asax)---


你会将静态var声明为---


public static int x;


或者你将它包装在一个存取器属性中as ---


private static int x;

public static int X

{

得到{return x; }

}


....为什么?


- Sahil Malik [MVP]
ADO.NET 2.0书 -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx

--------- -------------------------------------------------- -----------------

So here''s a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there''s web.config bla bla .. but lets just
say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

.... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------

推荐答案

包装方法确保在需要线程的情况下安全的

未来


问候
wrapper approach for sure in case that it requires to be thread safe in the
furture

regards


这是你编程风格的问题。一些程序员,包括我自己,

更喜欢不暴露变量成员并通过属性提供访问。

否则第一个表单没有任何问题。


Eliyahu


" Sahil Malik [MVP]" <共***************** @ nospam.com>在消息中写道

news:eT ************** @ TK2MSFTNGP12.phx.gbl ...
It''s a matter of your programming style. Some programmers, including myself,
prefer not to expose variable members and provide access via properties.
Otherwise there is nothing wrong with the first form.

Eliyahu

"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
所以这里是's相当简单的问题。

在ASP.NET应用程序中说,我希望在global.asax中分享常见常量作为静态变量(我知道有web.config bla bla。但是让
只是说我想使用global.asax)---

你会将静态var声明为---

public static int x;

或者你会把它包装在一个存取器属性中作为---

private static int x;
public static int X
{
get {return x; }

...为什么?

- Sahil Malik [MVP]
ADO.NET 2.0书 -
http://codebetter.com/blogs/ sahil.ma ... / 13 / 63199.aspx
------------------------------ --------------------------------------------
-
So here''s a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there''s web.config bla bla .. but lets just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
-------------------------------------------------------------------------- --



无论哪种方式都很好,如果没有代码必须验证变量X.显然

将它包装在一个属性中更多灵活。


现在,问题,为什么把它放在global.asax?如果是我,我会创建我自己的类,其中定义了变量。

" Sahil Malik [MVP]" <共***************** @ nospam.com>在消息中写道

news:eT ************** @ TK2MSFTNGP12.phx.gbl ...
Either way if fine, if no code has to validate the variable X. Obviously
wrapping it in a property is more flexible.

Now, the questions, why put it in global.asax? If it were me, I would
create my own class that had the variables defined.
"Sahil Malik [MVP]" <co*****************@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
所以这里是's相当简单的问题。

在ASP.NET应用程序中说,我希望在global.asax中分享常见常量作为静态变量(我知道有web.config bla bla。但是,让我们说我想使用global.asax)---

你会将静态var声明为---

public static int x;

或者你会把它包装在一个存取器属性中作为---

private static int x;
public static int X
{
get {return x; }

...为什么?

- Sahil Malik [MVP]
ADO.NET 2.0书 -
http://codebetter.com/blogs/ sahil.ma ... / 13 / 63199.aspx
------------------------------ ----------------------------------------------
So here''s a rather simple question.

Say in an ASP.NET application, I wish to share common constants as static
variables in global.asax (I know there''s web.config bla bla .. but lets
just say I wanna use global.asax) ---

Would you declare your static var as ---

public static int x ;

or would you wrap it up in an accessor property as ---

private static int x ;
public static int X
{
get { return x ; }
}

... and why?

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
----------------------------------------------------------------------------



这篇关于global.asax中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆