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
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
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
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
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
1