0

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.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
imujjwalanand
  • 309
  • 4
  • 15
  • 1
    Nope. Include all of those `using` statements in each file that requires them. – Robert Harvey May 13 '20 at 23:15
  • 3
    I don't think there's any way to do that, but you could [change the default using directives that Visual Studio adds automatically to every file](https://stackoverflow.com/a/651062/2791540) so that all of your using directives are included by default. That could save you a little typing I guess. – John Wu May 13 '20 at 23:17
  • I mean, select usings, Ctrl+C, create new class, select usings, Ctrl+V. I could have created 6 classes using that technique in the time it took to write the question... – Heretic Monkey May 13 '20 at 23:21
  • No, `C#` doesn't support `c`-style common header files or macros, something that could have perhaps achieved what you wanted –  May 13 '20 at 23:24

2 Answers2

0

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
-1

No, but IDEs usually handle imports quite well, but you can place all your classes in a single namespace and import that, even tho thats very bad practice.

Patrick Beynio
  • 788
  • 1
  • 6
  • 13