处理静态可变品c# [英] disposing static vairables c#

查看:63
本文介绍了处理静态可变品c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在建立一个在线考试网络应用程序。 Examination.aspx.cs类有很多静态可变量,int,string,float ...集合类型等我最近遇到了这个问题,我不知道该怎么做。在考试之后,我只需重新输入我的网页的位置并登录,然后再次开始测试。但我注意到静态变量仍然包含以前的值。我的问题,我应该在静态类... struct中定义这些变量吗?并实现IDisposable接口...或者只是将其重新初始化为0,null ??



我的代码示例...



Hello everyone, i am building an online Examination webapplication. The Examination.aspx.cs class has lots of static vairables, int,string,float...Collection types etc. I encountered this problem recently and i don''t know what to do about it. After giving an exam, i just retype the location of my webpage and log in and then start the test again. But i noticed that the static variables still contain the previous values. My question, should i define this variables in a static class...struct? and implement the IDisposable interface...or just re-initialize it to 0,null??

An example of my code...

public partial class Examination : System.Web.UI.Page
    {
        static int nQuestionIndex = 0;
        static int nExamType = 0;
        static int nTotalQuestions = 0;
        static int nTotalGeneralQuestions = 0;

        static EmployeeBO objEmpBO;
        static ReminderNotificationBO objReminderBO;
..............





先谢谢。



Thanks in Advance.

推荐答案

关于维护价值观:请了解这一点: http://msdn.microsoft.com/en-us/library/ms178581%28v=vs.100%29.aspx [ ^ ]。



但你尚未准备好。首先学习.NET。而且你需要学习静态类型和成员。注意静态方法本质上与静态数据有很大不同,并且相关。如果您需要帮助,我们很乐意为您提供帮助。作为一个规则,你很少应该使用静态数据,真的很少,但静态方法是非常平常和真正需要的,由于性能原因,当你不处理类/结构数据(这),只带参数或静态数据。



-SA
About maintaining values: Please learn this: http://msdn.microsoft.com/en-us/library/ms178581%28v=vs.100%29.aspx[^].

But you are not ready yet. Learn .NET in general first. And you need to learn static types and members. Pay attention that static methods are very different by their nature from static data, and related. If you need help on it, we would gladly help. As a rule of thumbs, you rarely should use static data, really rarely, but static methods are very usual and really required, due to performance reasons, when you don''t deal with class/structure data ("this"), only with parameters or static data.

—SA


你根本不知道.NET编程,几乎没有关于编程的粘合剂,需要在开始任何项目之前修复它。



int double 是原始的,他们根本不需要任何东西, string 和集合类不实现 System.IDisposable



混合处理和销毁存储在堆中的引用类型对象,回收堆内存。在CLR(托管)系统中,垃圾收集负责销毁和回收堆内存:

http ://en.wikipedia.org/wiki/Garbage_collection_(computer_science) [ ^ ]。



换句话说,你根本不需要做任何事情。请注意,作为代码作者不知道调用析构函数的时刻,依赖它是不正确的。实际上,在.NET中,很少使用析构函数。



处理机制通常与此无关。它用于许多特殊情况,通常与堆无关。其中一个例子是使用非托管资源并保证清理。这个接口的想法是任何性质的一些清理操作的自动化,但不回收托管内存。刚读一读:

http://msdn.microsoft.com /en-us/library/system.idisposable.aspx [ ^ ],

,顺便说一下, http://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ]。



-SA
You simply have no clue about .NET programming, and almost no glue about programming in general, need to fix it before starting any project.

The types like int and double are primitive, they don''t need anything at all, string and collection classes do not implement System.IDisposable.

You are mixing up disposal and destruction of reference-type objects stored in the heap, with reclaiming heap memory. In CLR (managed) systems, Garbage Collection takes care of destruction and reclaiming heap memory:
http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)[^].

In other words, you don''t need to do anything about it, anything at all. Note, that as the moment when a destructor is called is not known to the code author, it would be incorrect to rely on it. Actually, in .NET, destructors are rarely used.

Disposal mechanism is generally unrelated to this. It is used for many special cases, usually unrelated to heap. One such case is using unmanaged resources with guaranteed clean-up. The idea of this interface is automation of some clean-up action of any nature, but not reclaiming of the managed memory. Just read about it:
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[^],
and, by the way, http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

—SA


这篇关于处理静态可变品c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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