Manually destroy C# objects

You don’t manually destroy .Net objects. That’s what being a managed environment is all about. In fact, if the object is actually reachable, meaning you have a reference you can use to tell the GC which object you want to destroy, collecting that object will be impossible. The GC will never collect any object that’s … Read more

Combining foreach and using

foreach (ManagementObject result in results) using(result) { //code here } It’s not normally good practice to assign the variable outside the using block because the resource would be disposed but could stay in scope. It would, however, result in clearer code here because you can nested the using statement against the foreach. EDIT: As pointed … Read more

What does Process.Dispose() actually do?

Do I really need to Dispose() every Process object and how do I decide if I need to do so? Yes, you should dispose them. Note this text in the documentation for Process: A system process is uniquely identified on the system by its process identifier. Like many Windows resources, a process is also identified … Read more

What’s the point of overriding Dispose(bool disposing) in .NET?

The full pattern including a finalizer, introduction of a new virtual method and “sealing” of the original dispose method is very general purpose, covering all bases. Unless you have direct handles on unmanaged resources (which should be almost never) you don’t need a finalizer. If you seal your class (and my views on sealing classes … Read more

C# conditional using block statement

One option, which is somewhat nasty but would work, based on the fact that the C# compiler calls Dispose only if the resource is non-null: protected void ValidateExportDirectoryExists() { using (useNetworkAccess ? new Core.NetworkAccess(username, password, domain) : null) { CheckExportDirectoryExists(); } } Another alternative would be to write a static method which returned either null … Read more

Stream as a return value in WCF – who disposes it?

Service is responsible for closing stream and unless you change default behavior it does it automatically (the behavior with defalut values is always used). If you set OperationBehavior.AutoDisposeParameters to false you must register handler for OperationContext.OperationCompleted and dispose the stream in the handler as described here. Client cannot close the stream because client has a … Read more

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