How to avoid having to write namespace before class C#

Viewed 667

So I made a .dll which I added to my project everything works, but when I try to use any of the class from my .dll. I have to specificly use namespace.classname instead of being able to just say Classname even when I put at the top of my project

using namespace

using System;
using MyTestClassLibrary;
using System.IO;
using YangHandler;

namespace UsingMyclassdll
{
    class Program
    {
        static void Main(string[] args)
        {
            YangHandler.YangHandler yangh = YangHandler.YangHandler.Parse("Rawtext");

            Console.ReadKey();
        }
    }
}

At the line of using Yanghandler visual studio says

Using directive is unnecessary

Isn't this what using is exactly used for to use other namespaces?

YangHandler code

using System;
using System.IO;

namespace YangHandler
{
    public class YangHandler
    {
        public string YangAsRawText { get; private set; }

        public static YangHandler Parse(string YangAsRawText)
        {
            YangHandler handlerToReturn = new YangHandler();

            handlerToReturn.YangAsRawText = YangAsRawText;

            return handlerToReturn;
        }

I know that it could be solved by using namespace aliases under the namespace "UsingMyclassdll" like

using YangHandler = YangHandler.YangHandler;

But isn't there a more normal solution?

1 Answers

Check this very interesting piece of documentation from Microsoft: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces

DO NOT use the same name for a namespace and a type in that namespace.

For example, do not use Debug as a namespace name and then also provide a class named Debug in the same namespace. Several compilers require such types to be fully qualified.

So your work around is basically defining the fully qualified name as the type and namespace are of the same name.

No work around for this. The compiler can't know if you mean the one or the other.

Related