Global "using" directives in VS2010/C#?
Asked Answered
W

7

7

I'm pretty sure I know the answer but I'm wondering if there's a way to define a global "using" directive in my C# projects so that I don't have to repeat the directive on top of every code file.

My interest is really rooted with the introduction of extension methods in the .NET Framework. The only way to use an extension method is to define a using directive for the namespace containing the extension methods. Without the using directive, I lose Intellisense capabilities for the extension methods which means I won't always see what methods are available.

As a framework developer, making sure that the types and methods that are provided in the framework are clear and available to consuming developers is key to me. While documentation and training serve their purpose, I've found that most devs will hit the period and scroll through the Intellisense list to see what methods and properties are available. Even if they go to the Object Browser or view the reference documentation, they won't know about an extension method unless they know about it. This is where Intellisense comes in.

And, while I can add the using directive to the template VS uses, the "Remove and Sort" option in VS will remove the directive referencing the extension methods if one isn't being used.

So, all that being said, is there any way to define a global "using" directive in VS 2010? If not, any chance it's being considered by MS for the future?

Wommera answered 6/11, 2010 at 16:58 Comment(2)
This is self-induced by extension method abuse. Hard on the devs right now to find the proper #using. Murder a couple of years from now when somebody else has to read the code.Brandiebrandise
I disagree. Extensions methods serve a purpose but are only useful if you know they are there. By default, when there's no using directive, they aren't available. So it isn't a question of the dev finding the right using statement if they have to know about the method first. And, with the built-in Resolve functionality in VS as well as tools like ReSharper et al, finding the proper using directive is much easier.Wommera
G
6

You can place your extension methods in the global namespace, by making sure the containing classes are not declared within a namespace. This will make them globally available.

As for default namespaces - apart from changing the VS templates, this is not possible.

Of course, it is always possible the MS will consider it. Unlikely, but possible.

Instead of looking for a technical solution, perhaps you should look at educating your fellow developers and provide a convention regarding extension methods (for instance, a known location/namespace schema)?

Gaultiero answered 6/11, 2010 at 17:2 Comment(4)
I agree with your last paragraph; nobody (well, maybe mostly nobody) wants to use a framework that pollutes their global namespace, they want targeted usage of the framework when they want it and for it to disappear when it's not wanted.Rachelrachele
As I said, education and documentation only go so far and the vast majority of people (devs included) retain by applying. While we can provide training and reference material as well as catch instances during code reviews, it would still be nice if the methods were displayed in Intellisense. If ReSharper accomplishes this, as indicated below, then I think I'm okay.Wommera
@Wommera - as far as I know, Resharper does not automatically add using statements.Gaultiero
You now have the possibility to do this. Refer to my answer.Dasha
R
3

The global using directive is a new language feature in C# 10. When running on .NET 6 there are also implicit global using directives available in certain namespaces.

Rhinitis answered 21/10, 2021 at 9:58 Comment(0)
T
2

In VS 2010 there is no option to do it but in VS 2022 with C# 10.0 you can use Global Usings.

global using <TheNamespace>;
Tamishatamma answered 15/8, 2021 at 17:16 Comment(0)
D
1

Global using directives are introduced within C# 10.

It can be used through the same way as local using directives, except that you place a global keyword in front of it, in order to instruct the compiler to give access to the following directive accross the entire project:

global using System;

The recommended standard is to put all of your global using directives in a Usings.cs file to separate them from the rest of the project.

Dasha answered 10/1, 2022 at 20:49 Comment(0)
E
0

To answer your actual problem, if you put your static class with extensions in no namespace, it will be available everywhere. Use with care!

Eltonelucidate answered 6/11, 2010 at 17:12 Comment(1)
I hadn't thought about "hiding" the extension methods in this manner but I agree that it doesn't sounds like good practice.Wommera
H
0

If it's really important to you, you can have your company buy ReSharper and install it on all development machines, having this will give you Intellisense for those methods, with option to "auto add" the using directive.

From what I heard (didn't use it myself) the ReSharper scan all possible assemblies and I assume you can configure it to add "using" directive automatically when creating new class, though I'm not 100% sure in this.

Humbert answered 7/11, 2010 at 14:45 Comment(2)
I'll have to look at ReSharper again to see if it shows the extension methods in Intellisense even when there's no using directive. This would solve my issue and we do already have ReSharper in place on all dev systems. Unfortunately, it is ReSharper's built in organization feature that sorts and removes unused using directives on save that brought this situation to a head. But, if they are still displayed in Intellisense then I think all is good.Wommera
@Wommera - I believe it does, maybe only in newer version though.. :/Humbert
C
0

You can create .net file templates for classes that contain your using directives.

See Creating Project and Item Templates (https://msdn.microsoft.com/en-us/library/ms247121.aspx).

You can save the template as part of the project you have your extensions in; however, this is best done in a different project.

Crissman answered 20/3, 2017 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.