tags:

views:

121

answers:

7

Is there any objective reason not to remove unused default includes from C# program?
Let's take this hello world project as an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Hello world");
        }
    }
}

Program works fine with the

using System.Collections.Generic;
using System.Linq;
using System.Text;

part commented out.

While this example is very simple, I don't see at this point any good reason to keep those three using lines in program.

"I might need that later" comes to mind as a reason, but that would be true for any library which comes with compiler.

I just want to say that I'm a beginner in C#, so I may be missing something really obvious.

+6  A: 

No reason; I typically remove any "using" statements I'm not actually using. (And since I use ReSharper w/ Visual Studio, I can use their shortcuts for removing unused ones, and for adding them back if I use something form one of them in the future.)

Cleaner is better, in my opinion.

Adam Vandenberg
Yes. Resharper is cool. I also use it, and works great.
abhishek
I don’t see how you need Resharper for any of that.
Timwi
One doesn't need ReSharper for that; I use ReSharper for other reasons, and the fact that it *can* detect unused imports is just a nice bonus.
Adam Vandenberg
Visual Studio has this functionality built-in, you don't need Resharper to do it.
Fara
The annoying thing about that functionality is that it has no whitelist. It removes even the default namespaces I'd like to keep.
CodeInChaos
+4  A: 

1) They are usings and not includes, so they don't suffer a performance penalty like in C/C++

2) Intellisense won't list the classes in there

3) Extensionsmethods don't work. Particularly annoying in case of System.Linq since LINQ consists of extension methods.

CodeInChaos
He explicitly said he's not using them. That includes not using the extension methods.
Matthew Flaschen
Not yet. And the "blue box" automatic addition of usings doesn't work with extension methods. So he has to manually add the using again once he needs it.
CodeInChaos
@Matthew: He also says he's a beginner, which means that he may not *know* he's using them.
Adam Robinson
@Adam, the question says he tested without `using System.Linq;`, and it worked. That means he's correct about not using Linq.
Matthew Flaschen
+2  A: 

It makes it a little easier to use common features (LINQ, collections) later.

SLaks
+2  A: 

The less using constructs you have, the better, it reduces ambiguity in the program and makes it much easier to read when the reader understands the exact type that you return and take as arguments. In my opinion, there is no valid reason to keep them.

DeadMG
This answer is nonsensical. If any class name were ambiguous, the compiler would complain about it. Having `using` directives that don’t cause the program to stop compiling is not an ambiguity and has no effect on readability.
Timwi
The compiler only complains if it finds a clash. Just because the compiler knows that type X is in namespace A.B.C.D.E, doesn't mean that the code makes that clear. Especially bad when you find examples on the Internet, but they only post part and don't tell you what they've used.
DeadMG
+1  A: 

You can remove these using statements if you don't like but there is no harm to keep as it is.

It totally depends n you , how you wanna see your code base?

saurabh
+1  A: 

I usually leave the using statements there until I am somewhat "done" with writing the class (I know a class isn't perfect first time).

Then there are options in Visual Studio context menu to a) remove unused Using statements, and b) sort Using statements. This applies to Visual Studio with no add-ons.

Simon Bartlett
+1 for context menu. It seems like I never noticed that it's there!
AndrejaKo
+4  A: 

I regularly run the "remove unused usings and sort" over my entire codebase - one of my easy red-flags when reviewing a class file is too many using statements. It's normally an indication that the class I'm looking at is worrying itself with too many concerns.

Matt Reeve