16

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?

7
  • 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
    Commented Oct 16, 2012 at 13:46
  • 2
    Also read stackoverflow.com/a/151560/240613: in your code, usings are in the wrong place. Commented Oct 16, 2012 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
    Commented Oct 16, 2012 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
    Commented Oct 16, 2012 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
    Commented Oct 16, 2012 at 17:10

5 Answers 5

50

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 you can safely remove the ones you don't need. You will have to add any back that you subsequently need to reference in any subsequent code you write.

If you right click and select Organize Usings > Remove and Sort it will delete any that are unused. There are also extensions that will remove and sort the namespaces automatically on saving each file.

5
  • 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
    Commented Oct 16, 2012 at 14:09
  • 11
    @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. Commented Oct 16, 2012 at 15:03
  • 4
    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
    Commented Oct 16, 2012 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
    Commented Oct 17, 2012 at 3:17
  • This does not really answer "...and if they are really necessary. Can I leave these out? " but is a "how to" Commented Aug 3, 2020 at 13:05
5

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.

2
  • Happy to help the question that you asked puzzled me for a long time too. :) Commented Oct 16, 2012 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
    Commented Oct 17, 2012 at 3:15
3

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.

4
  • But are they really necessary? Is there a good reason for them to be in the template?
    – Onno
    Commented Oct 16, 2012 at 13:47
  • If you don't need them, simply remove them. They are only defaults.
    – Bernard
    Commented Oct 16, 2012 at 13:48
  • 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.
    – GHP
    Commented Oct 16, 2012 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. Commented Oct 16, 2012 at 16:57
0

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.

-1

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

1
  • 1
    Please do not try to add unnecessary (and potentially spammy) links to your answers.
    – ChrisF
    Commented Oct 23, 2012 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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