Skip to main content

How to Resolve Error "CS0138: A 'using namespace' directive can only be applied to namespaces" in C#

The Compiler Error CS0138 is a syntax error regarding directives. The message reads: "A 'using namespace' directive can only be applied to namespaces; 'TypeName' is a type not a namespace. Consider a 'using static' directive instead."

In C#, the standard using directive (e.g., using System;) is designed to import entire Namespaces (collections of classes) so you don't have to type fully qualified names. It is not designed to import a specific Class, Struct, or Enum. If you try to using a specific class name, the compiler rejects it because a class is a data type, not a namespace container.

This guide explains how to correctly import static members or create aliases for types.

Understanding Namespaces vs. Types

  • Namespace: A logical container (like a folder) that holds classes. Example: System.
  • Type: A definition for an object (Class, Struct, Enum). Example: System.Console or System.Math.

You can say "Open the System folder" (using System;). You cannot say "Open the Math class" using the same command. You must be more specific about how you want to open the class.

Scenario 1: Importing Static Members (The 'using static' Fix)

The most common cause of CS0138 is trying to import a class so you can use its static methods (like Math.Sqrt or Console.WriteLine) without typing the class name every time.

Example of error

Trying to import System.Math as if it were a namespace.

// ⛔️ Error CS0138: 'System.Math' is a type not a namespace.
using System.Math;

public class Calculator
{
public double GetRoot(int number)
{
return Sqrt(number); // We want to use Sqrt directly
}
}

Solution: Use using static

Introduced in C# 6.0, the using static directive tells the compiler: "Import all static members (methods and properties) of this specific type."

// ✅ Correct: Added the 'static' keyword
using static System.Math;

public class Calculator
{
public double GetRoot(int number)
{
// Now valid because all static members of 'Math' are available globally in this file
return Sqrt(number);
}
}

Scenario 2: Importing Enums

Enums are also Types, not namespaces. If you want to access Enum members (like Color.Red) without typing the Enum name every time, you cannot use a standard using.

Example of error

// ⛔️ Error CS0138: 'System.ConsoleColor' is a type (Enum), not a namespace.
using System.ConsoleColor;

public class Painter
{
public void Paint()
{
var c = Red; // Want to use 'Red' instead of 'ConsoleColor.Red'
}
}

Solution

Use using static for Enums as well.

// ✅ Correct: Import the static members of the Enum
using static System.ConsoleColor;

public class Painter
{
public void Paint()
{
var c = Red;
}
}

Scenario 3: Creating Type Aliases

Sometimes you don't want to import all static members; you just want to shorten a long class name or resolve a naming conflict. In this case, using static is not the answer; creating an Alias is.

Example of error

// ⛔️ Error CS0138: 'MyCompany.Project.VeryLongClassName' is a type.
using MyCompany.Project.VeryLongClassName;

Solution: Assign an Alias

Use the assignment syntax = to give the type a local nickname.

// ✅ Correct: Defining an alias named 'ShortName'
using ShortName = MyCompany.Project.VeryLongClassName;

public class Program
{
static void Main()
{
// Use the alias to create instances
ShortName instance = new ShortName();
}
}

Conclusion

CS0138 occurs when you confuse a container (Namespace) with an item (Type).

  1. Check the Name: Is the name you are importing a Class, Struct, or Enum?
  2. Determine Intent:
    • Do you want to call methods like Sqrt() directly? Use using static TypeName;.
    • Do you want to shorten the name of the class? Use using Alias = TypeName;.
    • Do you just want to use the class without the full path? Remove the class name from the directive and import only its Namespace.