Thursday, August 29, 2019

dispose - Implementing IDisposable in C#




I am trying to implement IDisposable in a sample program. If I use SqlConnection class inside a using block statement, it will automatically dispose it.




public int testCon()
{
using (SqlConnection conn = new SqlConnection("Conn string"))
{
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "SELECT COUNT(1) FROM Carsd";

return (int)cmd.ExecuteScalar();

}
}
}


I have created a class and implemented IDisposable. I have created a new instance inside a using block statement.



class Program 
{
static void Main(string[] args)

{
testDispose objTestDispose;

using (objTestDispose = new testDispose())
{
objTestDispose.UserName = "testUser";
objTestDispose.PassWord = "testPassword";
}

Console.WriteLine("Check obj of testDispose Class" + objTestDispose.UserName);

Console.WriteLine("Check obj of testDispose Class" + objTestDispose.PassWord);
Console.ReadLine();

}
}

public class testDispose : IDisposable
{
public string UserName { get; set; }
public string PassWord { get; set; }


public void Dispose()
{ }
}


I believe, using block automatically call dispose method. So, if I am create a new instance in using block, it would be dispose after existing using block. But, still I am able to access objTestDispose object outside of the using block.WHY?



Please suggest.




UDPATE



Mr.BWA..Thank you for the making my question duplicate. but you should know I am a student and learning. I have this question in my mind so I have asked here.
**You can not say that IDisposable interface only for unmanaged resources.**I can also remove managed resources. It depends on the situation. As per the below link -




What if your object has allocated a 250MB System.Drawing.Bitmap (i.e. the .NET managed Bitmap class) as some sort of frame buffer? Sure, this is a managed .NET object, and the garbage collector will free it. But do you really want to leave 250MB of memory just sitting there – waiting for the garbage collector to eventually come along and free it? What if there's an open database connection? Surely we don't want that connection sitting open, waiting for the GC to finalize the object.



If the user has called Dispose() (meaning they no longer plan to use
the object) why not get rid of those wasteful bitmaps and database

connections?



So now we will:



get rid of unmanaged resources (because we have to), and get rid of
managed resources (because we want to be helpful)



Answer



Dispose is being called, but it doesn't do anything to destroy the object itself (you'll note that a lot of IDiposable classes within the Framework additionally have a IsDisposed property to indicate whether the unmanaged resources have been released or not)


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...