简体   繁体   English

asp.net在应用程序范围内以三种方式声明全局变量/类。 哪一个最好?

[英]asp.net declaring global variable/class in application scope 3 ways. Which one is best?

I am looking for differences between those 3 ways of using static class in asp.net application scope. 我正在寻找在asp.net应用程序范围内使用静态类的这3种方式之间的差异。 Will all of these point to the same class? 所有这些都指向同一个类吗? Which one is preferable >object< defined declaratively inside global.asax or static class ? 在global.asax或static类内以声明方式定义的> object <哪一个更可取?

examples: 例子:

<object runat="server" scope="application" class="classname" ID="objID"></object>

VS VS

public static class classname {}

VS VS

Application("a") = new classname();

I'm assuming that your edit means: 我假设您的编辑意思是:

In global.asax.cs: 在global.asax.cs中:

Application["a"] = new classname();

In classname.cs 在classname.cs中

public static class classname {}

In which case, they're much the same with the big exception that (classname)Application["a"] will refer to an instance of the classname class, whereas in the second example any calls to classname.MethodNameGoesHere() will be calling static methods of the classname class. 在这种情况下,它们几乎相同,但有一个大例外: (classname)Application["a"]将引用classname类的实例 ,而在第二个示例中,对classname.MethodNameGoesHere()任何调用都将被调用classname类的静态方法。

I'd go for static methods of a static class, rather than methods of an instance stored in Application, although I'd then think very carefully about what the methods in classname do, as they could be being called simultaneously as many people could be accessing the site at once. 我会选择静态类的静态方法,而不是Application中存储的实例的方法,尽管那时我会非常仔细地考虑classname中的方法的作用,因为可能会同时调用它们,因为可能会有很多人立即访问该网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM