静态是否以这种方式表现 [英] Does static behave this way

查看:86
本文介绍了静态是否以这种方式表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




请仔细阅读以下代码

展开 | 选择 | Wrap | 行号

解决方案

静态基本上意味着只创建一个此对象并在整个过程中使用它。我相信其他人会详细说明,但你可以通过在MSDN上查找来获得更详细的了解。这只是想到它的简单方法。


i是一个具有整个类范围的变量,因为它不是在你的一个方法中创建的。因此,当你用第一个按钮点击它将它设置为5时,它仍然存在,直到该类被处理掉。因此,接下来的两种方法看到它的值为5。


只要该类保持活动状态,变量i仍然存活。


是的我同意这一点,但是如果我们移动到下一页或关闭应用程序时,类对象将被关闭(通过垃圾收集器,或者我错了),那么值如何变量i坚持。


垃圾收集远非瞬间。

如果你想强迫它你可以.Dispose()你的班级或者你可以调用GC.Collect ()

Hi,

Kindly go through the code below

Expand|Select|Wrap|Line Numbers

解决方案

Static basically means "create only one of this object and use it throughout". I''m sure someone else will elaborate on that, but you could get a more detailed understanding yourself by looking it up on MSDN. This is just the easy way to think of it.

i is a variable that has the scope of the entire class because it is not created within one of your methods. So of course when you set it to 5 with the first button click it remains that until the class is disposed of. Thus you see it''s value of 5 with the next two methods.

So long as that class remains alive, the variable i remains alive.


Yes I do agree with that, But if we move to the next page or when I close the application, the class object will be disposed off(through the Garbage Collector, or am I wrong), then how does the value of the variable "i" persist.


Garbage Collection is far from instantaneous.
If you want to force it you could .Dispose() of your class or you could call GC.Collect()


这篇关于静态是否以这种方式表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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