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

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

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); }

Questions about Entity Framework Context Lifetime

Let’s get controversial! I disagree with the general MVC + EF consensus that keeping a context alive throughout the entire request is a good thing for a number of reasons: Low performance increase Do you know how expensive creating a new database context is? Well… “A DataContext is lightweight and is not expensive to create” … 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 Statement with Generics: using ISet = System.Collections.Generic.ISet

Unfortunately, the using directive does not do what you want. You can say: using Frob = System.String; and using ListOfInts = System.Collections.Generic.List<System.Int32>; but you cannot say using Blob<T> = System.Collections.Generic.List<T> or using Blob = System.Collections.Generic.List It’s a shortcoming of the language that has never been rectified.

yield return statement inside a using() { } block Disposes before executing

When you call GetAllAnimals it doesn’t actually execute any code until you enumerate the returned IEnumerable in a foreach loop. The dataContext is being disposed as soon as the wrapper method returns, before you enumerate the IEnumerable. The simplest solution would be to make the wrapper method an iterator as well, like this: public static … Read more

Why is “using namespace X;” not allowed at class/struct level?

I don’t know exactly, but my guess is that allowing this at class scope could cause confusion: namespace Hello { typedef int World; } class Blah { using namespace Hello; public: World DoSomething(); } //Should this be just World or Hello::World ? World Blah::DoSomething() { //Is the using namespace valid in here? } Since there … Read more

tech