0

If I have no using statements in my C# file and I write e.g. return Task.CompletedTask; the IntelliSense feature in VS will complain that Task is not recognized. In order to fix this I need to add the using statement using System.Treading.Tasks;.

Instead of typing it out I use the VS shortcut to fix this, but it always adds it at the top of the file, outside of my defined namespace. I know R# has a feature to import it inside the namespace, but can VS 2017 without R# support this? Or is there any extension etcetera which will support this?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Johan S
  • 3,531
  • 6
  • 35
  • 63
  • why do you care? – pm100 Feb 21 '18 at 16:52
  • 1
    @pm100 https://stackoverflow.com/questions/125319/should-using-statements-be-inside-or-outside-the-namespace – Alok Feb 21 '18 at 16:58
  • Are you aware of the fact that the import is only visible inside the importing file? Normally you have only one namespace per file. So restricting the visibility of your import to the namespace becomes equivalent to having it restricted to the file. So there is no advantage of doing so... – oliver Feb 21 '18 at 17:07
  • 2
    I think you meant [using ***directive***](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive), not [using statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement). – NightOwl888 Feb 21 '18 at 17:08
  • @oliver The link Alok provided discusses the difference between having the using directive inside, or outside of the namespace. It changes the order in which type resolution happens inside that file. Personally, I use it as justification for why using directives should always be outside of the namespace, but that's a choice each developer/group of developers has to make. If you do want that type resolution order, then it is advantageous to put it inside the namespace. –  Feb 21 '18 at 17:22
  • 1
    @Andy: I see, didn't know that. – oliver Feb 21 '18 at 17:26
  • @Alok Wow - so VS generates code that stylecop flags as bad. Seems like left hand not knowing what right hand is doing. – pm100 Feb 21 '18 at 18:52
  • My stylecop at works forces me to do this, please focus at the question instead of discussing the code style, you can post a separate question to do this. – Johan S Feb 21 '18 at 21:17

0 Answers0