Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you resolve .Net namespace conflicts with the 'using' keyword?

Tags:

Here's the problem, you include multiple assemblies and add 'using namespaceX' at the top of your code file.
Now you want to create a class or use a symbol which is defined in multiple namespaces, e.g. System.Windows.Controls.Image & System.Drawing.Image

Now unless you use the fully qualified name, there will be a crib/build error due to ambiguity inspite of the right 'using' declarations at the top. What is the way out here?

(Another knowledge base post.. I found the answer after about 10 minutes of searching because I didn't know the right keyword to search for)

like image 940
Gishu Avatar asked Sep 14 '08 10:09

Gishu


People also ask

How to resolve namespace conflict c#?

Change the Aliases property from Global to TST. At the top of the namespace declarations in your code add "extern alias TST"; You can now either fully qualify any references to TwitterClientInfo. Or add using statements with an alias like below.

Can namespace and class have same name?

Inside a namespace, no two classes can have the same name.


2 Answers

Use alias

using System.Windows.Controls; using Drawing = System.Drawing;  ...  Image img = ... //System.Windows.Controls.Image Drawing.Image img2 = ... //System.Drawing.Image 

How to: Use the Namespace Alias Qualifier (C#)

like image 62
aku Avatar answered Oct 07 '22 18:10

aku


This page has a very good writeup on namespaces and the using-statement:

http://www.blackwasp.co.uk/Namespaces.aspx

You want to read the part about "Creating Aliases" that will allow you to make an alias for one or both of the name spaces and reference them with that like this:

using ControlImage = System.Windows.Controls.Image; using System.Drawing.Image;  ControlImage.Image myImage = new ControlImage.Image(); myImage.Width = 200; 
like image 36
Espo Avatar answered Oct 07 '22 18:10

Espo