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?
5 Answers 5
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.
-
2Also, 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– Mr Lister2012年10月16日 14:09:14 +00:00Commented 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.Steven Evers– Steven Evers2012年10月16日 15:03:04 +00:00Commented Oct 16, 2012 at 15:03
-
4Also 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– Holger2012年10月16日 21:41:15 +00:00Commented 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– Job2012年10月17日 03:17:19 +00:00Commented 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"Mark Schultheiss– Mark Schultheiss2020年08月03日 13:05:44 +00:00Commented Aug 3, 2020 at 13:05
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.
-
Happy to help the question that you asked puzzled me for a long time too. :)Akshat Jiwan Sharma– Akshat Jiwan Sharma2012年10月16日 14:07:03 +00:00Commented 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– Job2012年10月17日 03:15:57 +00:00Commented Oct 17, 2012 at 3:15
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.
-
But are they really necessary? Is there a good reason for them to be in the template?Onno– Onno2012年10月16日 13:47:58 +00:00Commented Oct 16, 2012 at 13:47
-
If you don't need them, simply remove them. They are only defaults.Bernard– Bernard2012年10月16日 13:48:20 +00:00Commented 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– GHP2012年10月16日 15:19:50 +00:00Commented 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.Wyatt Barnett– Wyatt Barnett2012年10月16日 16:57:26 +00:00Commented Oct 16, 2012 at 16:57
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.
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
-
1Please do not try to add unnecessary (and potentially spammy) links to your answers.ChrisF– ChrisF2012年10月23日 07:53:33 +00:00Commented Oct 23, 2012 at 7:53
usings
in the default templates, so unless Microsoft updates those sometime soon this question is valid.