C# Interfaces – Using Keyword in C# Interface

cinterfaceskeywords

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?

Best Answer

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.