Microsoft KB Archive/316734

= Compiler error occurs when you duplicate namespace names =

Article ID: 316734

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005 Express Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q316734



SYMPTOMS
When you compile your project, you may receive a compiler error that states that a method or a property does not exist in a specific namespace.

You may notice that the namespace that the error refers to is a namespace that you created, not the common language runtime namespace that you intended to use.



CAUSE
This behavior occurs when you name a custom namespace by using the same name as one of the common language runtime namespaces.



RESOLUTION
When you choose names for the custom namespaces in your project, make sure that you do not duplicate namespaces that already exist in the common language runtime or in other referenced assemblies. If you duplicate namespace names, code in your namespace resolves to your custom namespace and not to the namespace in the common language runtime.

If you cannot rename your custom namespace, you can use an alias for that namespace. For more information about how to create an alias for your custom namespace, see the steps in the &quot;More Information&quot; section.



STATUS
This behavior is by design.



Visual C# 2005 or Visual C# .NET
 Open a new Console Application project in Microsoft Visual C# 2005 or in Microsoft Visual C# .NET. Add a new class to the project.  Replace the code in Class2.cs with the following code.

Note In Visual C# 2005, the default file is Program.cs. using System; namespace Test.System {   class X     { } }                    Add another new class to the project.  Replace the code in Class3.cs with the following code.

Note In Visual C# 2005, the new file is Class1.cs. using System; namespace Test.Lists {   class Y    { public static void test {           System.Console.WriteLine(&quot;Hello&quot;); }   } }                    </li> Build the project. You receive the following error message:

The type or namespace name 'Console' does not exist in the class or

namespace 'Test.System' (are you missing an assembly reference?)

</li>  To create an alias for this custom namespace, use the following code. using MySystem = System; namespace Test.Lists {   class Y    { public static void test {       MySystem.Console.WriteLine(&quot;Hello&quot;); }   } }                    </li></ol>

Visual Basic 2005 or Visual Basic .NET
<ol> Open a new Console Application project in Microsoft Visual Basic 2005 or in Microsoft Visual Basic .NET.</li> Add a new class to the project.</li>  Replace the code in Class1.vb with the following code. Namespace Test.System

Class X   End Class End Namespace </li> Add another new class to the project.</li>  Replace the code in Class2.vb with the following code. Imports System Namespace Test.Lists

Class Y       Public Sub test System.Console.WriteLine(&quot;Hello&quot;) End Sub End Class

End Namespace </li> Build the project. You receive the following error message:

'Console' is not a member of 'System'

</li>  To create an alias for this custom namespace, use the following code. Imports MySystem = System Namespace Test.Lists

Class Y       Public Sub test MySystem.Console.WriteLine(&quot;Hello&quot;) End Sub End Class End Namespace </li></ol>

Additional query words: namespace collision CLR

Keywords: kbtshoot kbvs2005applies kbvs2005swept kbcompiler kbprb KB316734

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.