Sometimes the simplest things can be the coolest. I just stumbled onto this today, I cannot believe I did not know about it before (or if I did, I had forgotten).
In C#, in addition to simply importing a namespace into a code file (“using” directive), you can also define an alias for it. Why is this cool?
A hypothetical project uses assemblies from two vendors. For argument's sake lets say the two vendors are just seperate departments in your organization. Both departments maintain their own business object classes, in their own namespaces:
namespace MyCompany.Accounting.BusinessClasses {...}
and
namespace MyCompany.Shipping.GlobalDataClasses.Core {...}
Now, it doesn't really matter what the actual namespaces are for this discussion, just that they are a pain in the ass to type, and they both contain a class of the same name (”Company” in this example).
Now, supposing we work for the Accounting department and we are asked to write a peice of code that can find and update a corresponding Shipping account record when someone in Accounting saves a change on our side. We would likely end up with something like this...
using MyCompany.Accounting.BusinessClasses;
using MyCompany.Shipping.GlobalDataClasses.Core;
<...>
MyCompany.Accounting.BusinessClasses.Company Destination = MyCompany.Accounting.BusinessClasses.Company.FindCompany(company_id);
MyCompany.Shipping.GlobalDataClasses.Core.Company Source = new MyCompany.Shipping.GlobalDataClasses.Core.Company(company_id);
<... do some stuff with both objects ...>
Now that's enough verbosity to make the baby Jesus cry! Having to inline those namespaces in the code makes it VERY painful to read, and the only reason we have to do it is because otherwise the type name “Company” would be too ambiguous.
Well, Namespace Aliases gives us a way to “pretty up” this code. We can assign an alias to each namespace import, which can then be used in the code as an abbreviation:
using Acct=MyCompany.Accounting.BusinessClasses;
using Ship=MyCompany.Shipping.GlobalDataClasses.Core;
<...>
Acct.Company Destination = Acct.Company.FindCompany(company_id);
Ship.Company Source = new Ship.Company(company_id);
<... do some stuff with both objects ...>
Fantastic!
One other thing to mention is that when you alias a namespace like this, you can no longer directly reference classes from that namespace without a qualifier. For example:
using Data=System.Data;
<...>
DataSet MyDataSet1 = new DataSet(); // <-- this will not work when System.Data is aliased!
Data.DataSet MyDataSet2 = new Data.DataSet(); // <-- this will work
What you can do to fix this is to import the namespace twice. Once using the alias, and once without an alias. This gives us the best of both worlds:
using Data=System.Data;
using System.Data;
<...>
DataSet MyDataSet1 = new DataSet(); // <-- this will work now!
Data.DataSet MyDataSet2 = new Data.DataSet(); // <-- this will still work
Cool stuff!!! I love learning new simple language features I should have known about 2 years ago!!