What is the Significance of Dispose?

Last post 07-06-2009 4:11 AM by sirdneo. 5 replies.

Sort Posts:

  • What is the Significance of Dispose?

    07-05-2009, 12:34 AM
    • Member
      8 point Member
    • ankumar
    • Member since 07-05-2009, 4:04 AM
    • London
    • Posts 21

    Hi, What is the significance of Dispose, finalize and using? What the situation they use because I have been asked in interview? Please help me out.

  • Re: What is the Significance of Dispose?

    07-05-2009, 9:21 AM
    • Member
      549 point Member
    • adeelehsan
    • Member since 07-18-2005, 10:56 AM
    • UAE
    • Posts 94

    Hello

    Whenever we create an object in .NET, it takes resources for example the database connection. After using it we should destroy it so that it releases the memory. Although it is done automatically by garbage collector in .NET at some stage but doing it yourself is always a best practise.

    Example

    con.Dispose()

    We can also use using {} block to automatically dispose the object at the end so we do not have to call Dispose() explicitly.

    for example

    using (SqlConnection con)

    {

    ........................

    //No need to call Dispose()

    }//con is disposed automatically after this.

    Please Mark AS Answer if it helped.
    Regards
    ADEEL EHSAN
  • Re: What is the Significance of Dispose?

    07-05-2009, 9:22 AM
    Answer
    • Member
      549 point Member
    • adeelehsan
    • Member since 07-18-2005, 10:56 AM
    • UAE
    • Posts 94

    Hello

    Whenever we create an object in .NET, it takes resources for example the database connection. After using it we should destroy it so that it releases the memory. Although it is done automatically by garbage collector in .NET at some stage but doing it yourself is always a best practise.

    Example

    con.Dispose()

    We can also use using {} block to automatically dispose the object at the end so we do not have to call Dispose() explicitly.

    for example

    using (SqlConnection con)

    {

    ........................

    //No need to call Dispose()

    }//con is disposed automatically after this.

    Please Mark AS Answer if it helped.
    Regards
    ADEEL EHSAN
  • Re: What is the Significance of Dispose?

    07-05-2009, 9:40 AM
    Answer
    • Participant
      989 point Participant
    • Scott927
    • Member since 09-20-2006, 4:53 AM
    • Phoenix, AZ
    • Posts 181

    Finalize and Dispose are both methods available to a programmer to aid in the Garbage Collection process (which ensures that memory is freed up as soon as possible when an object goes out of scope).

    Finalize :
    1 .Finalize() is called by the runtime.
    2. Is a destructor, called by Garbage Collector when the object goes out of scope.
    3. Implement it when you have unmanaged resources in your code, and want to make sure that these resources are freed when the Garbage collection happens.

    Dispose :
    1. Dispose() is called by the user and implemented via the IDisposable interface.
    2. Same purpose as finalize, to free unmanaged resources. However, implement this when you are writing a custom class, that will be used by other users and you want to allow them to call Dispose() to clean up and managed resources that were consumed during the objects lifetime.
    3. Overriding Dispose() provides a way for the user code to free the unmanaged objects in your custom class.
    4.
    There is no performance benefit in implementing the Dispose method on types that use only managed resources (such as arrays) because they are automatically reclaimed by the garbage collector.

    Using:
    Using is simply a block of code that provides a syntax that ensures the correct usage of IDisposable objects. For a full description, see this link:

    http://msdn.microsoft.com/en-us/library/yh598w02.aspx

    Scott M Schluer
    ---
    MCPD: ASP.NET Developer 3.5
  • Re: What is the Significance of Dispose?

    07-06-2009, 3:17 AM
    Answer
    • All-Star
      24,144 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,406

    They're all commonly used when working with unmanaged resources. They clean up such resources before destroying them. Here's a good article with examples and explanations that might bring all of them in perspective and help you understand better: http://codeverity.com/timweaver/usingclosedisposeandfinalize/

    Regards,
    Peter
  • Re: What is the Significance of Dispose?

    07-06-2009, 4:11 AM
    • Contributor
      7,266 point Contributor
    • sirdneo
    • Member since 12-16-2008, 12:45 AM
    • Karachi, Pakistan
    • Posts 1,141

    See the article

    http://www.devx.com/dotnet/Article/33167


    It tells in detail when to use Dispose and Finalize

    Thanks,
    Zeeshan Umar

    ~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~

    My Blog
Page 1 of 1 (6 items)