另一个新手问题 [英] Another novice question

查看:53
本文介绍了另一个新手问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天前讨论过静态方法并得到了一些很好的答案

非常有用。


现在我不确定是否有关在成员(变量)和

类上使用静态。


有人可以给我一些关于静态类和成员的建议吗?
< br $>
谢谢


Doug

解决方案

Gordon,


正如你自己的规则,如果你*可以*避免它们,而不是避免它们。


他们永远保留占用的内存,因此你应该在

我的意见是你确定你需要它们无休止的时间和

不需要更多的实例。


(请注意,ASPNET应用程序的其他行为比

WindowForms应用程序有关静态的行为,因为窗体是一个

客户端,而ASPNET应用程序是中间层)


如何永远(不是后面的句子),只是我的想法。


Cor


" gordon" < go ********** @ optusnet.com.auschreef in bericht

news:44 ****************** ****@news.optusnet.com.au ...


>我几天前讨论过静态方法,得到了一些很好的答案。 />非常有用。


现在我不确定是否在成员(变量)和

类上使用静态。


有人可以给我一些关于静态课程和会员的建议

呢?


谢谢


Doug



我不确定我是否会像Cor一样强烈...静态成员确实

有他们的位置......但是如果你在一个类上有静态字段[fields = class level

变量],那么你需要考虑的最重要的事情就是

线程安全 - 不是一个微不足道的主题。


简单的静态助手不与静态字段对话的函数是

内在线程安全的,但是一旦涉及字段,你需要

意识到这是非常重要的应用程序不同的线程可能同时使用

静态类,因此需要了解其影响...


当然,如果你*知道*你不打算使用线程,然后

OK ;-p


Jon Skeet有一篇关于线程的非常好的文章 - 请注意它进入很多

页......
http://www.yoda.arachsys.com/csharp/threads/

Marc


< blockquote>这没有任何意义。静态方法不会保留任何内存占用超出拨号时分配的堆栈。


但是,当方法退出时,堆栈的内存已经消失了。


静态字段是一个不同的故事,是的,它们会持续很长时间

申请域名持续。但是,说你不应该使用它们

是一个过早的优化,因为实际上,它们可能指向一个

对象,以及参考的大小不高。即使对象很大,实例成员也可以引用大型对象,并且也可以在应用程序域中使用,具体取决于使用情况。


此外,ASP.NET不会以任何不同的方式处理静态字段。静态

字段在应用程序域的生命周期中生效,不多也不少。当然,

ASP.NET重新定义应用程序域,并且必须要注意这一点,但静态

字段没有发言权,它是ASP的运行时。确定

的NET。


最后,OP询问静态方法,而不是静态字段。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam .guard.caspershouse.com


" Cor Ligthert [MVP]" < no ************ @ planet.nlwrote in message

news:ev ************** @ TK2MSFTNGP04.phx .gbl ...


Gordon,


正如你自己的规则,如果你*可以*避免它们,避免它们。


它们永远保留了占用的内存,因此你应该使用它们

我认为你确定你需要它们无休止的时间和

不需要更多实例。


(请注意,ASPNET应用程序还有其他行为,而不是

关于静态的WindowForms应用程序,仅仅因为窗体是一个

客户端,而ASPNET应用程序是中间层)


但是(不是后面的句子),只是我的想法。


Cor


" gordon" < go ********** @ optusnet.com.auschreef in bericht

news:44 ****************** ****@news.optusnet.com.au ...


>>我几天前讨论过静态方法,得到了一些很好的答案
非常有用。

现在我不确定是否在成员(变量)和
类上使用静态。

有人可以请给我一些静态课程和成员的建议

谢谢

道格




I aksed a few days ago about static methods and got some good answers that
were really useful.

Now I am not sure if about the use of static on members (variables) and
classes.

Can someone please give me some advice about static classes and members too?

Thanks

Doug

解决方案

Gordon,

Just as rule for yourself, if you *can* avoid them, than avoid them.

They keep forever the memory occupied and therefore you should use them in
my opinion there were you are sure that you needs them endless times and
don''t need more instances of them.

(Be aware that there is other behaviour for ASPNET applications than for
WindowForms applications about static, just because a windowforms is a
client while an ASPNET application is a middle tier)

However (not the later sentence), just my idea.

Cor

"gordon" <go**********@optusnet.com.auschreef in bericht
news:44**********************@news.optusnet.com.au ...

>I aksed a few days ago about static methods and got some good answers that
were really useful.

Now I am not sure if about the use of static on members (variables) and
classes.

Can someone please give me some advice about static classes and members
too?

Thanks

Doug



I''m not sure I''d put it quite as strongly as Cor... static members do
have their place... but if you have static fields [fields=class level
variables] on a class, then the biggest thing you need to consider is
thread safety - not a trivial subject.

Simple static "helper" functions that don''t talk to static fields are
intrinsicly thread-safe, but as soon as fields are involved you need to
be aware that in a non-trivial app different threads may be using the
static class at once, so the impact needs to be understood...

Of course, if you *know* you aren''t going to be using threading, then
OK ;-p

Jon Skeet has a very good article on threading - note it goes into many
pages...
http://www.yoda.arachsys.com/csharp/threads/

Marc


This doesn''t make any sense. A static method doesn''t keep any memory
occupied beyond the stack that is allocated when the call is made.

However, when the method exits, the memory for the stack is gone as
well.

Static fields are a different story, and yes, they will last as long as
the application domain lasts. However, saying that you shouldn''t use them
is a premature optimization, since in reality, they might be pointing to an
object, and the size of the reference is not high. Even if the object is
big, instance members can reference large objects, and live for the life of
an app domain as well, depending on the use.

Also, ASP.NET does not treat static fields any differently. Static
fields live for the life of the app domain, no more, no less. Granted,
ASP.NET recylces app domains, and one has to be aware of that, but static
fields have no say in that, it is the runtime of ASP.NET that determines
that.

In the end, the OP asked about static methods, not static fields.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ev**************@TK2MSFTNGP04.phx.gbl...

Gordon,

Just as rule for yourself, if you *can* avoid them, than avoid them.

They keep forever the memory occupied and therefore you should use them in
my opinion there were you are sure that you needs them endless times and
don''t need more instances of them.

(Be aware that there is other behaviour for ASPNET applications than for
WindowForms applications about static, just because a windowforms is a
client while an ASPNET application is a middle tier)

However (not the later sentence), just my idea.

Cor

"gordon" <go**********@optusnet.com.auschreef in bericht
news:44**********************@news.optusnet.com.au ...

>>I aksed a few days ago about static methods and got some good answers that
were really useful.

Now I am not sure if about the use of static on members (variables) and
classes.

Can someone please give me some advice about static classes and members
too?

Thanks

Doug




这篇关于另一个新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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