Odd .NET Namespace Scoping Causes Compile-Time Exception

1. January 2011

This isn’t really a bug in the .NET compilers, but it sure is an interesting namespace scoping issue that could easily be encountered. If this teaches you anything, be it that you should never name a nested namespace level the same name as one of its parents in the hierarchy.

In C#

First here’s the Compile Time Exception that I was getting:

The type or namespace name ‘Data’ does not exist in namespace ‘MyApp.Data.MyApp’ (are you missing an assembly reference?)

At first I was like “What do you mean namespace ‘Data’ doesn’t exist in ‘MyApp.Data’?”

Here’s the C# code that causes this exception:

namespace MyApp.Data
{
    public partial class Person
    {
        public string Name { get; set; }
    }
}

namespace MyApp.Data.MyApp
{
    public partial class Billy : MyApp.Data.Person
    {

    }
}

If you are familiar with scoping of namespaces in .NET then you probably can see what the issue is. Remember, I has this issue in a large solution with multiple projects and it wasn’t as straight forward as this. This example is the most basic example that will raise this exception.

Basically, since the ‘Billy’ class is within the ‘MyApp.Data.MyApp’ namespace, all references to the ‘MyApp’ namespace will be scoped to the nested ‘MyApp’ instead of the root.  So, as a consequence, the ‘MyApp.Data.Person’ object doesn’t exist because the compiler is really looking for ‘MyApp.Data.MyApp.Data.Person’.

How about VB.NET?

Another thing to point out is that I also tested this in VB.NET, and the VB.NET compiler gives a similar compile-time exception message for the same ‘MyApp.Data.Person' reference within the ‘Billy’ class. Here’s VB.NET’s message:

Type ‘MyApp.Data.Person’ is not defined

Here’s the VB.NET code:

Namespace MyApp.Data
    Public Class Person


    End Class
End Namespace

Namespace MyApp.Data.MyApp
    Public Class Test
        Public Sub New()
            Dim person = New MyApp.Data.Person()
        End Sub
    End Class
End Namespace

Conclusion

You may assume, as I did at first, that the compiler should just fall back to the ‘MyApp’ root namespace if it can’t find the type within the nested namespace. This assumption was completely incorrect, and after thinking it over, the compilers response does make sense.

I actually never ran into this before, so I thought I would share.

C#, vb.net , ,

Comments

1/6/2011 2:11:51 PM #
For online casino lovers and slots lovers this is a sterling opportunity to get their hands on some exclusive bonus codes pre-approved at party casino.
1/8/2011 5:42:58 AM #
thanks for this nice tips
1/8/2011 5:50:10 AM #
Good news, I'd thank to writer coz' i've found a lot useful knowledge. I've added this blog to my RSS reader Smile. Bye!Smile
1/8/2011 6:16:38 AM #
Hello,great article. Informations are pretty usefull and saves me huge amount of time which I   spend on something else instead of googling Smile Thanks a lot!
1/8/2011 7:52:22 AM #
Tobacco marketing is us smokeless turning into more and more restricted all over the world. Tobacco marketing is lorillard Tobbacco turning into more and more restricted all over the world. Tobacco industries say that the critics more than exaggerate the results of their advertising. They also stage out that teenage smoking can also be growing in nations exactly where most types of marketing have already been banned. Tobacco goods are dangerous. Don't be concerned smokers, no one desires to lock you up.
1/8/2011 11:04:11 AM #
About the recent case about a stolen sword being returned to Brown U... There is another possiblity in that it was robbed from the Institution by a undergrad or professor, and then sold to the immediate owners. This would spare the Tharpe family of any proactive wrongdoing and there should be a way in a fair world to find the prime bandit. If the judge orders that the sword be given back to the university (if it is theirs), there should be a settlement or coverage of some sort. After all, it is not the Tharpes liability if the university cannot maintain adequate protection, and it may even be sue-able by the Tharpes for such, ie their lack of protection made them fall victim to acquiring stolen wares.
1/8/2011 4:42:42 PM #
I like this blog, nice!
1/8/2011 11:13:47 PM #
April 2011 - Win an Porsche Boxster Spyder at the dollarcarclub.com Free Sweepstakes
1/8/2011 11:44:00 PM #
It looks like a fake to relaunch an out-of-business dating platform like Meezoog. Practically nothing mentions TPB on that web site, neither to the Meezoog's weblog: on its approximate Facebook Connect application, Pirate Date (counting just 2 fans, correct now) cites TPB for its "concept", but I wasn't in a position to locate any announcement about an official partnership with TPB or one of its founders. When you notice, Pirate Date doesn't display the actual TPB logo: the ship was a minor modified including an hearth on the sails and that font household inside the header could effortlessly be employed by any individual. Fail.If you have time.Please enter my blog <a href="http://www.buybestratedlaptops.info/">best rated laptops</a>