0

Yes, I realize Garbage Collector takes care of freeing up memory, and I also know of the inefficiencies in clearing up memory manually, however for educational purposes I would like to know how to dispose a class instance in VB.Net.

3
  • 1
    Implement the IDisposable interface on your class and call .Dispose() when you want to be rid of it. Commented Aug 29, 2013 at 6:16
  • @Adrian Could you please post some code and post it as an answer so I can give you credit? Commented Aug 29, 2013 at 6:27
  • 1
    Note that free up memory is not the same as dispose. You can't manually free the memory a class instance takes up. Commented Aug 29, 2013 at 7:04

1 Answer 1

1

look at the example given in MSDN: http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

to use it, you can either use Using block or call Dispose() directly. for example:

' use Using Block: 
Using r = New MyResource(100)
    Console.WriteLine(r.ToString())
End Using

' call Dispose directly:
Dim r = New MyResource(100)
Console.WriteLine(r.ToString())
r.Dispose()
Sign up to request clarification or add additional context in comments.

1 Comment

Not all the classes implement the IDisposable interface, being unable to use the .Dispose(). I think the OP asks for a (not recommended) way to do it with the GC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.