0

I have a class "Extensions" that is defined in namespace Backend and is static class Extensions.

In it there is a method public static string MakePath(params string[] paths). I usually use the class for extension methods but this one isn't such.

Yet if I were to call it from another class like so MakePath("asd", "asd") I get a "does not exist in the current context" error without the option to add a "using" statement.

Fully qualifying it, like so Extensions.MakePath fixes that but I'd prefer a using directive. Putting using Backend; at the top of the file does nothing. Visual Studio simply does not associate it like it should. Never had such a problem before.

How can I fix this so that Visual Studio would know "MakePaths" is in the class "Extensions" in the namespace "Backend" and a "using" directive would be detected, so that I can use the method, without fully qualifying it?

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
J. Doe
  • 363
  • 2
  • 11
  • Note that calling the class "extensions" is misleading. An extension method is a very [specific thing in C#](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods). – DavidG Jan 21 '20 at 11:22
  • _using static Backend.Extensions;_ See [using directive](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive) – Steve Jan 21 '20 at 11:23
  • @DavidG, well, it's full of extension methods, so... :D – J. Doe Jan 21 '20 at 11:26
  • Not really though, they're just methods. An extension method would extend a class, and would have a `this` parameter. – DavidG Jan 21 '20 at 11:26
  • Just making sure... You're aware of `Path.Combine`, right? – canton7 Jan 21 '20 at 11:27
  • @DavidG ...yeah, that's exactly the kind of methods that class is full of. – J. Doe Jan 21 '20 at 11:37
  • 1
    @canton7, yes, but in my experience it has NEVER been useful or worked right. This method correctly concatenates strings with ending backslashes. – J. Doe Jan 21 '20 at 11:38
  • 1
    @J.Doe .NET Core 2.1 and .NET Standard 2.1 introduced [`Path.Join`](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.join?view=netcore-3.1) to fix that complaint – canton7 Jan 21 '20 at 12:34
  • @canton7 thanks, didn't know about that one! :) – J. Doe Jan 22 '20 at 10:41

1 Answers1

3

You can solve it by the following directive using static Backend.Extensions and call your method like MakePath("asd", "asd");, without class name. using static was introduced in C# 6.0

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
  • Huh, thanks! Is it because it's a static class that makes `using` different and creates this problem? – J. Doe Jan 21 '20 at 11:25
  • @J.Doe I think, that initial cause of your issue is that you have another `Extensions` class or namespace (more likely) in your solution, it's hard to tell without context. But `using static` is helpful, when you want (or need) to call a static method without specifying a class name – Pavel Anikhouski Jan 21 '20 at 11:27
  • @J.Doe Before `using static`, you were *never* able to invoke a static method from a different class without using the class name, or an instance method from another class without invoking it on an instance – canton7 Jan 21 '20 at 11:28