Why should Dispose() be non-virtual?

Typical usage is that Dispose() is overloaded, with a public, non-virtual Dispose() method, and a virtual, protected Dispose(bool). The public Dispose() method calls Dispose(true), and subclasses can use this protected virtual method to free up their own resorces, and call base.Dispose(true) for parent classes. If the class owning the public Dispose() method also implements a … Read more

Should I manually dispose the socket after closing it?

Calling Close internally calls Dispose so you don’t need to call both. From .NET Reflector: public void Close() { if (s_LoggingEnabled) { Logging.Enter(Logging.Sockets, this, “Close”, (string) null); } ((IDisposable)this).Dispose(); if (s_LoggingEnabled) { Logging.Exit(Logging.Sockets, this, “Close”, (string) null); } } If possible you should use the using pattern so that you always call Dispose regardless of … Read more

Do I need to Dispose a SemaphoreSlim?

If you access the AvailableWaitHandle property, then Yes, you must call Dispose() to cleanup unmanaged resources. If you do not access AvailableWaitHandle, then No, calling Dispose() won’t do anything important. SemaphoreSlim will create a ManualResetEvent on demand if you access the AvailableWaitHandle. This may be useful, for example if you need to wait on multiple … Read more

using statement FileStream and / or StreamReader – Visual Studio 2012 Warnings

The following is how Microsoft recommends doing it. It is long and bulky, but safe: FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using (TextReader tr= new StreamReader(fs)) { fs = null; // Code here } } finally { if (fs != null) fs.Dispose(); } This method will always ensure … Read more

What happens if I don’t call Dispose on the pen object?

A couple of corrections should be made here: Regarding the answer from Phil Devaney: “…Calling Dispose allows you to do deterministic cleanup and is highly recommended.” Actually, calling Dispose() does not deterministically cause a GC collection in .NET – i.e. it does NOT trigger a GC immediately just because you called Dispose(). It only indirectly … Read more

C# how to implement Dispose method

Question 1: Implement IDisposable as well, using the following pattern: public class MyClass : IDisposable { bool disposed; protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { //dispose managed resources } } //dispose unmanaged resources disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } } Question 2: What Microsoft means … Read more

Dispose vs Dispose(bool)

IDisposable provides a method with the signature public void Dispose() Microsoft best practices (Implement a Dispose method) recommend making a second private method with the signature private void Dispose(bool) Your public Dispose method and finalizer should call this private Dispose method to prevent disposing managed resources multiple times. You can fix the warning you are … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)