Search code examples
c#namespacesusing

How can I Import A Namespace from A File in A Seperate Folder in C#


How do I bring into the current namespace an extension method (see below) into scope, when the file this class is defined in is in another folder?

When StringHandler.cs is in the Product folder, I can include using Utilities; in Product.cs, but when it is in a separate folder, alongside the Project folder (in the Utilities folder), I can't figure out how to include it. Can someone please explain how I can use the using keyword in this case? For instance, where is using really pointing to on my file system (do I need to specify the using directory relative to my csproj file or to my CallCenter.sln file?)?

│   CallCenter.sln
│
├───src
│   ├───Project
│   │   │   Product.cs
|   |   |   Project.csproj
│   │   ├───bin
│   │   │   └───...
│   │   │
│   │   └───obj
│   │       └───...         
│   │
│   └───Utilities
│           StringHandler.cs

StringHandler.cs

namespace Utilities
{
    public static class StringHandler
    {
        public static string InsertSpaces(this string source)
        {
            string result = string.Empty;

            if (!string.IsNullOrWhiteSpace(source))
            {
                foreach (char letter in source)
                {
                    if (char.IsUpper(letter))
                    {
                        result = result.Trim();
                        result += " ";
                    }
                    result += letter;
                }
            }
            return result.Trim();
        }
    }
}

Solution

  • The folder in which a .cs file is located has no meaning during compilation. The using statement just allows types from a certain namespace to be found. To be able to use the extension method, make sure that:

    • The namespace is included (using Utilities;)
    • The file (StringHandler.cs) is included in your project or in another project that your current project has a reference to