Is a namespace but is used like a type

On this page

    Introduction

    This error is a common challenge when developing with C#. In C#, you define namespaces to scope classes. You can name both namespaces and classes. But, a class must not have the same name as its namespace.

    :::note What you need to know

    • You named a type the same name as the namespace that it is in.
    • Make sure that types have unique names that are different than the namespace.

    :::

    What causes the "is a namespace but is used like a type" error?

    The compiler will throw this error after two conditions are met. First, you need to create a type with the same name as a namespace. This is the root cause. 

    ```

    namespace MyNamespace

    {

        public class MyNamespace { }

    }

    ```

    :::note Hmm

    P.S. Look at how stupid this looks. Sorry, that’s mean. This looks real smart.

    :::

    Unfortunately, the compiler won’t throw an error right away. The compiler sits, silently waiting for you to do something foolish. It’s waiting for you to try to access that type in an ambiguous way.

    ```

    // This is a bad namespace

    // Because it has the same name

    // As a type in the namespace

    using MyAwesomeThing;

    namespace MyAwesomeThing

    {

        public class MyAwesomeThing

        {

            public MyAwesomeThing() { }

        }

    }

    // This is a good namespace

    // Because it has a different name

    // Than any type in the namespace

    namespace NormalStuff

    {

        public class NormalThing

        {

            // Unfortunately, this will throw an error.

            MyAwesomeThing.MyAwesomeThing myAwesomeClass = new MyAwesomeThing();

        }

    }

    ```

    The compiler is confused. What the hell is going on? Which MyAwesomeThing does MyAwesomeThing refer to? Why is Unity telling me that “'MyAwesomeThing' is a namespace but is used like a type”?

    How do I fix the "is a namespace but is used like a type" error?

    It’s easy to fix the “is a namespace but is used like a type” error.

    Rename your type so that it doesn’t share the same name as your namespace.

    ```

    namespace MyNamespace

    {

        public class MyClass { }

    }

    ```

    This is much better.

    ```

    // This is a good namespace (now)

    // Because it has a different name

    // Than any type in the namespace

    using MyAwesomeThing;

    namespace MyAwesomeThing

    {

        public class MyClass

        {

            public MyClass() { }

        }

    }

    // This is a good namespace

    // Because it has a different name

    // Than any type in the namespace

    namespace NormalStuff

    {

        public class NormalThing

        {

            // Fortunately, this will not throw an error.

            MyAwesomeThing.MyClass myAwesomeClass = new MyClass();

        }

    }

    ```

    Recommended reading

    I strongly recommend that you read this series from Eric Lippert on namespace and type naming collisions. It’s great.

    Conclusion

    Hope this was a helpful quick look into namespace and type naming. You should now be well-equipped to avoid the ‘is a namespace but is used like a type’ error in the future.

    Join our newsletter to get the latest updates
    Sign Up
    Share
    Michael Sacco
    Founder & CEO

    Michael Sacco is the Founder and CEO of OccaSoftware where he specializes in developing game assets for Unity game developers. With a background ranging from startups to American Express, he's been building great products for more than 10 years.

    Tags
    c sharp
    unity
    unity troubleshooting
    Unity Errors