Why remove unused using directives in C#?

There are a few reasons you’d want to take them out. It’s pointless. They add no value. It’s confusing. What is being used from that namespace? If you don’t, then you’ll gradually accumulate pointless using statements as your code changes over time. Static analysis is slower. Code compilation is slower. On the other hand, there … Read more

what does `using std::swap` inside the body of a class method implementation mean?

This mechanism is normally used in templated code, i.e. template <typename Value> class Foo. Now the question is which swap to use. std::swap<Value> will work, but it might not be ideal. There’s a good chance that there’s a better overload of swap for type Value, but in which namespace would that be? It’s almost certainly … 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

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

Using the using statement in C# [duplicate]

The using syntax can(should) be used as a way of defining a scope for anything that implements IDisposable. The using statement ensures that Dispose is called if an exception occurs. //the compiler will create a local variable //which will go out of scope outside this context using (FileStream fs = new FileStream(file, FileMode.Open)) { //do … Read more