静态类概述
我读了一些关于静态类的文章,但我有一些问题:
静态类在内存中的位置在哪里?
根据C#静态方法:
表示您希望声明引用单个位置
如果我在 Asp.Net 中使用静态类,这是否意味着我有一个在所有请求之间共享的位置?我在文章中读到静态类和静态方法比面向实例的类更快,所以为什么我没有在每个地方看到它们。我的意思是在.Net中也存在一些静态类和方法。
I read some articles about static classes but I have some questions:
Where take static classes place in memory?
According to C# Static Methods :
means that you want the declaration to refer to a single location
If I use static classes in Asp.Net is it mean I have a location that share between all requests?I read in the articles that static classes and static methods are faster that instance-oriented classes,so why I don't see them in every where.I mean in .Net there is a few static classes and methods exist too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.
所以这意味着静态类被加载到内存中。这意味着您不希望在进程启动时将所有类加载到内存中。
2.asp.net中的静态类保存在应用程序状态中,所以是的,您有一个在所有请求之间共享的位置。
静态方法在 ASP.NET 中很有用,但前提是您不在方法内部使用静态对象,因为不同的线程访问修改相同的变量可能会遇到麻烦。
3.如果您使用大量静态类,它们将被加载到内存中,而这不一定是您想要的。
请参阅此链接了解更多信息:
asp.net/C# 中的静态变量
1.
So it means that the static classes are loaded in memory. This mean that you would not like that all your classes are loaded in memory when your process starts.
2.Static classes in asp.net are saved in application state, so yes, you have a location that share between all requests.
Static methods are useful in asp.net but only if you don't use static objects inside of the methods because you may get in trouble with different threads accessing modifying the same vaiable.
3.If you use a lot static classes they will be loaded in memory and that's not necesarly something you would want.
See this link for more information:
static variables in asp.net/C#