How to Resolve Error "CS0105: The using directive for 'namespace' appeared previously in this namespace" in C#
The Compiler Warning CS0105 is a code cleanup warning. The message reads: "The using directive for 'namespace' appeared previously in this namespace".
This warning indicates that you have imported the same namespace more than once within the same scope. While this does not prevent your code from compiling or running correctly, it is considered "code smell." It clutters your file and indicates that code may have been blindly copy-pasted or merged without cleanup.
This guide explains how to identify and remove these redundant directives.
Understanding the Warning
The using directive (e.g., using System;) tells the compiler to look in a specific namespace for types that you haven't fully qualified. Once you tell the compiler to look in System, telling it a second time in the same file (or scope) provides no new information.
The compiler flags this to help you keep your code concise and maintainable.
Scenario 1: Duplicate Lines (Copy-Paste Errors)
The most common cause is simply having the same line written twice at the top of the file. This often happens during merge conflicts or when copying code snippets from Stack Overflow that include their own using statements.
Example of error
using System;
using System.Collections.Generic;
// ⛔️ Warning CS0105: The using directive for 'System' appeared previously in this namespace
using System;
public class Program
{
// ...
}
Solution
Simply delete the duplicate line.
using System;
using System.Collections.Generic;
// ✅ Correct: Each namespace is imported only once.
public class Program
{
// ...
}
Scenario 2: Scoping Redundancy (Outer vs. Inner)
C# allows using directives to be placed inside a namespace block. However, if a directive is placed at the top of the file (Global/File scope), it automatically applies to all namespaces defined inside that file. Defining it again inside the braces is redundant.
Example of error
// Scope A: File Level
using System.IO;
namespace MyApp
{
// Scope B: Namespace Level (Inherits everything from Scope A)
// ⛔️ Warning CS0105: 'System.IO' is already imported by the line above.
using System.IO;
public class FileHandler
{
public void Read()
{
File.ReadAllText("data.txt");
}
}
}
Solution
Remove the inner directive. The outer one is sufficient.
using System.IO;
namespace MyApp
{
// ✅ Correct: No redundancy.
public class FileHandler
{
public void Read()
{
File.ReadAllText("data.txt");
}
}
}
Style Tip: Most C# style guides recommend keeping all using directives at the very top of the file (outside the namespace declaration) to keep the code clean, unless you specifically need to limit the scope of a directive to avoid a naming collision.
Solution: Cleanup and "Organize Usings"
You do not need to hunt these down manually. Visual Studio and other IDEs have built-in tools to fix this instantly.
In Visual Studio:
- Right-click anywhere in your code editor.
- Select Remove and Sort Usings.
- Alternatively, use the shortcut:
Ctrl+R, thenCtrl+G.
This will automatically delete duplicates (CS0105) and remove unused imports (CS8019), leaving your file clean.
Conclusion
CS0105 is a harmless but messy warning.
- Check the top of your file: Look for identical lines.
- Check inside namespaces: Ensure you aren't re-importing something declared at the file level.
- Automate: Use "Remove and Sort Usings" to fix this project-wide.