简体   繁体   中英

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. Will all of these point to the same class? Which one is preferable >object< defined declaratively inside global.asax or static class ?

examples:

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

VS

public static class classname {}

VS

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

I'm assuming that your edit means:

In global.asax.cs:

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

In 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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