Can “using” with more than one resource cause a resource leak?

No. The compiler will generate a separate finally block for each variable. The spec (ยง8.13) says: When a resource-acquisition takes the form of a local-variable-declaration, it is possible to acquire multiple resources of a given type. A using statement of the form using (ResourceType r1 = e1, r2 = e2, …, rN = eN) statement … Read more

Do I have to Close() a SQLConnection before it gets disposed?

Since you have a using block, the Dispose method of the SQLCommand will be called and it will close the connection: // System.Data.SqlClient.SqlConnection.Dispose disassemble protected override void Dispose(bool disposing) { if (disposing) { this._userConnectionOptions = null; this._poolGroup = null; this.Close(); } this.DisposeMe(disposing); base.Dispose(disposing); }

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

How do I use the C#6 “Using static” feature?

It appears the syntax has slightly changed since those blog posts were written. As the error message suggests, add static to your include statement: using static System.Console; // ^ class Program { static void Main() { WriteLine(“Hello world!”); WriteLine(“Another message”); } } Then, your code will compile. Note that, in C# 6.0, this will only … Read more