保持对象null比实现Idisposable Interface更好 [英] keeping object null is better than implementing Idisposable Interface

查看:93
本文介绍了保持对象null比实现Idisposable Interface更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace A1
{
    class A
    {
        public int m;
        public int n;
        public int F(A a)
        {
            a.m = 50;
            a.n = 40;

            return (a.m*a.n);
        }
    }
}

using A1;
namespace try_catch_Idisposabe
{
    class B
    {
        public static void Main(string[] args)
        {
            A a1=new A();
            try
            {
                a1.m = 10;
                a1.n = 30;
                int l = a1.F(a1);
                Console.WriteLine(l);
            }
            catch
            {
            }
            finally
             {
                    a1=null;
             }
        }
    }
}

推荐答案

在这个例子中,你的班级A不需要实现IDisposable:它不包含任何需要处理的内容。

但是,它很简单:MSDN确实提供了一个guid,告诉你如何做到这一点正确实施IDisposable [ ^ ]



但是你的标题不正确 - 设置对 null 的引用并不像处理类实例那样做,并且它决不会做任何更好的事情。如果您在类中使用稀缺资源并且未实现IDisposable,则将引用设置为 null 不会释放这些资源 - 它们将一直处于使用状态,直到调用垃圾收集器为止并删除类实例,并释放它的资源 - 这可能不会在非常长的时间内发生,并且它通常仅在内存不足时触发,而不是资源。



例如,如果你的类在它的构造函数中打开一个文件,并在它的Dispose方法中关闭它,那么将引用设置为 null 将使文件保持打开状态使用直到垃圾收集器销毁您的类实例。在此之前,应用程序内部或外部的任何内容都无法访问该文件。
In that example, your class "A" does not need to implement IDisposable: it does not contain anything which needs to be Disposed.
However, it is simple enough to do: MSDN does provide a guid which shows you how to do it properly Implement IDisposable[^]

You title however is not correct - setting a reference to null does not do the same thing as Disposing of a class instance, and it in no way does anything "better". If you use scarce resources in your class and do not implement IDisposable, then setting the reference to null does not release those resources - they remain in use until the Garbage Collector is called in and removes class instance, and frees it's resources - which may not happen for a very, very long time, and it is only normally triggered when memory is running low, not resources.

For example, if your class opens a file in it's constructor, and closes it in it's Dispose method, then setting the reference to null will leave the file open and in use until the Garbage Collector destroys your class instance. Until then, nothing inside or outside your application can access the file.


当您要处理临时对象时,请使用 using语句。

使用:定义一个范围,在其范围之外将放置一个或多个对象。(来自MSDN)

Ex:



Use "using" statement when you want to deal with temporary object.
Using :"Defines a scope, outside of which an object or objects will be disposed."(From MSDN)
Ex :

public static void main()
{
    using(A objA = new A())
    {
       //Your code related to object goes here. 
    }
}  





探索使用声明。

与您的搜索相关的问题在这里......

http://stackoverflow.com/questions/567138/when-should-i-use-using-blocks-in-c [ ^ ]



希望这个帮助

-----------------

Pratik Bhuva



Explore about using statement.
the question related to your search is here...
http://stackoverflow.com/questions/567138/when-should-i-use-using-blocks-in-c[^]

Hope This Help
-----------------
Pratik Bhuva


这篇关于保持对象null比实现Idisposable Interface更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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