Tuesday 28 November 2017

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 href="https://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface">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)



class="post-text" itemprop="text">
class="normal">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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...