Search code examples
c#namespacesusing-directives

Encapsulate using directive statements C#


I have a number of using directives declaring the namespaces I am going to use in multiple classes. Is there a way to refer multiple using statements via a single using directive or any other approach to this solution that I can implement for all the classes? The classes/files might or might not be under the same namespace. Example :

//Fil1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;

namespace NS1
{
    public class1
    {
    }
}

The reason to encapsulate these using statements is because they'll be common among multiple files.

//Fil2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;

namespace NotNS1
{
    public class2
    {
    }
}

Would prefer if there were some way to just make one using directive call in the class and define all these using statements elsewhere. Thanks.


Solution

  • Theres no magic using or #include like in the C world. You have to be explicit in each file or namespace what you are using.

    Many tools, including Visual Studio, ReSharper and Rider include tools to help manage using directives.