Is a namespace but is used like a type

by Michael Sacco Published September 1, 2023

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.

Quick help

  • 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 { }
}

Hmmm…

  • P.S. Look at how dumb it looks when you name a class the same name as the namespace. That’s how you know it’s a bad idea.
  • 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.

using MyAwesomeThing;

// This is a bad namespace
// Because it has the same name
// As a type in the namespace
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.

Free download: Indie Game Marketing Checklist

Download now

Category

c sharp

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free