Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;
             }
        }
    }
}
Posted
Updated 24-Oct-13 2:11am
v2
Comments
rishimuni 24-Oct-13 8:09am    
How can I implement Idisposable interface for my class alo

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.
 
Share this answer
 
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 :

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


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
 
Share this answer
 
v3
Comments
phil.o 25-Oct-13 5:37am    
This is only possible if class A implements IDisposable interface.
Pratik Bhuva 25-Oct-13 5:50am    
yes sir but i think its batter to use using instead of setting it null explicitly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900