Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When I'm using C# to write some code and I define an interface using Visual Studio 2010, it always includes a number of "using" statements (as shown in the example)

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

namespace TestEngine.TestNameSpace
{
    interface ITest1
    {
        bool testMethod(int xyz);
    }
} 

I wonder what these are for and if they are really necessary. Can I leave these out? Are they only necessary when I'm using those parts in my interface description?

share|improve this question
    
thnx for the edit. I just figured it out myself. somehow the using statements were left out even though I had selected them in the code block :) –  Onno Oct 16 '12 at 13:46
2  
Also read stackoverflow.com/a/151560/240613: in your code, usings are in the wrong place. –  MainMa Oct 16 '12 at 14:15
1  
Thnx for the link, it's a great addition. After the answer by @Akshat Jiwan Sharma it became clear that this is indeed the case, but at the same time the question pertains to the default template/settings as provided by MS when you install VS2010. –  Onno Oct 16 '12 at 14:17
1  
@MainMa - this is where Visual Studio puts the usings in the default templates, so unless Microsoft updates those sometime soon this question is valid. –  ChrisF Oct 16 '12 at 15:05
1  
@Midhat: True, but I think it's evident what is being asked when one reads the question itself. Feel free to edit the title if you can think of a better one. –  Onno Oct 16 '12 at 17:10
show 3 more comments

5 Answers

up vote 49 down vote accepted

Visual Studio doesn't know what code you intend to write so includes the most common namespaces for you by default in the "new class" template. This is done so you don't have to resolve all the references for every single line of new code you write.

Once you have written your basic code if you right click and select Organize Usings > Remove and Sort it will delete any that are unused.

share|improve this answer
5  
+1 for remove and sort usings –  jk. Oct 16 '12 at 13:57
2  
Also, if you do remove a "using" line by mistake, you can add them back in by going to the class name that now can't be found and right-click -> Resolve -> Using... –  Mr Lister Oct 16 '12 at 14:09
10  
@MrLister: If you didn't already know this: press "ctrl+." (control period) when your cursor is at any unresolved type or class and you get the same effect without ever having to touch the mouse. –  Steve Evers Oct 16 '12 at 15:03
3  
Also just as an addition to the answer: unused "usings" won't affect the runtime of your code, as only the used "usings" will be loaded. They can however affect IDE and compile speeds: stackoverflow.com/questions/136278/… –  Holger Oct 16 '12 at 21:41
    
Since this is the accepted answer, I wanted to mention that StyleCop will complain about references being in the wrong place and also one can edit a template for a new class and a new interface, etc. –  Job Oct 17 '12 at 3:17
add comment

These default using statements are part of your default template when creating interfaces. You can always edit the template to have them removed. See this question for more details.

share|improve this answer
    
But are they really necessary? Is there a good reason for them to be in the template? –  Onno Oct 16 '12 at 13:47
    
If you don't need them, simply remove them. They are only defaults. –  Bernard Oct 16 '12 at 13:48
    
+1 for the link to the template editing –  Onno Oct 16 '12 at 13:59
    
They are only necessary if one of your interface methods is a return type located in one of them. You need System at a minimum, and the others only as needed for types you want to return. –  Graham Oct 16 '12 at 15:19
    
Actually, many classes don't need system. But all in all it makes life easier on most developers, especially those not horribly used to .NET and not knowing where to pick up the main system classes. –  Wyatt Barnett Oct 16 '12 at 16:57
add comment

Yes you can remove any using directive that is not being used.These directives are automatically added by visual studio since they are the most commonly used ones and if for instance you are not going to use linq in the interface then you can remove the directive

 System.Linq;

The same holds true for other directives as well.Also it is a good practice in my opinion to move the using directives inside namespaces.

share|improve this answer
1  
+1 for the nice explanation :) –  Onno Oct 16 '12 at 14:05
    
Happy to help the question that you asked puzzled me for a long time too. :) –  Akshat Jiwan Sharma Oct 16 '12 at 14:07
    
One can edit a template file and adjust which using statement get included and where - they should go inside of the namespace by default or else StyleCop will complain, and even worse - bad things can happen. –  Job Oct 17 '12 at 3:15
add comment

In short, they are called namespaces. Namespaces are also used to reference some library classes within your code. There is also a recommended guidelines on how to use namespaces in .NET

In most cases, you may simple remove them if not used. The following two namespaces are potential candidates to to be removed.

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

Visual Studio will complain and NOT compile the code, once some framework/library classes have been used without declaring their namespaces. Thus, namespace play roll of structuring your code as well as easing the references between the project classes, interfaces, constructs, etc.

share|improve this answer
add comment

using in C# used for many ways.. as word keyword using means using.. on start when we used using system; that means there is a DLL of name System in which all main functions are stored and we use it to short our code...

system.WriteLine("Andy");

but when we use system above then we can write it as

WriteLine("Andy");

more over in SQLserver when we use using connection it means after we use data from database by using command connection will itself close. it is very useful for a coder who wants to save its time

share|improve this answer
1  
Please do not try to add unnecessary (and potentially spammy) links to your answers. –  ChrisF Oct 23 '12 at 7:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.