Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing namespaces globally?

Is there a way to reference a namespace globally across the whole solution?

So instead of having these lines in every code file:

using System;
using MyNamespace;

having to declare them only once, and every code file would use them.

Btw I am using Visual Studio.

like image 244
Joan Venge Avatar asked Jun 04 '09 18:06

Joan Venge


1 Answers

No, C# doesn't have this concept. Each source file is independent in this respect. (And if the using directives are in a namespace declaration, those are independent from other using directives in peer namespace declarations, too. That's a pretty rare case though in my experience.)

You don't need ReSharper to change what gets included in a new class though. You can use the Visual Studio templates.

EDIT: Just to clarify the point about using directives within namespaces, suppose we had (all in one file):

using Foo;

namespace X
{
    using Bar;
    // Foo and Bar are searched for code in here, but not Baz
}

namespace Y
{
    using Baz;
    // Foo and Baz are searched for code in here, but not Bar
}

Usually I only have one namespace declaration in a file, and put all the using directives before it.

like image 153
Jon Skeet Avatar answered Sep 28 '22 06:09

Jon Skeet