C# and VB.NET - Differing namespace conventions
Recently, while reviewing the VB.NET translation of the MVC Music Store tutorial, I noticed that none of the controllers / models / classes in general have namespaces. I was going to blow up on the person who did the translation like a bad FxCop, but I fired up a new MVC 3 / VB.NET app and saw the project template doesn't use namespaces, and that the New Controller wizard doesn't use a namespace, etc. After checking with the ASP.NET team, I decided to post the VB.NET source code without explicit namespaces in each class, because that's how the code would look if you followed the steps in the tutorial.
I'm sure developers who work with both VB.NET and C# regularly know this; I pretty much moved from VB6 to C# and this was news to me, so I thought I'd share it.
When you're creating a new class in a C# application, the class will have a namespace that matches its path in the application. The default project structure for an ASP.NET MVC application has a Controllers directory, so all controller classes in the MvcMusicStore application (for example) are in the MvcMusicStore.Controllers namespace.
using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using MvcMusicStore.Models; namespace MvcMusicStore.Controllers { public class HomeController : Controller { public ActionResult Index() { // ...
Note: that's just the default. You can use any namespace you'd like; ASP.NET MVC doesn't use the namespace / directory location in locating your controllers.
Controller classes created using the "Add Controller" tooling in Visual Studio will look like this:
Public Class HomeController Inherits Controller Public Function Index() As ActionResult ' ...
However, if you reflected over the assembly, you'd see that the HomeController class is in the MvcMusicStore namespace, as are all other classes in the assembly. That's because in VB.NET you have a default namespace that's set in the project properties, and that namespace is applied to all the generated assemblies.
I'd have guessed that I could drop a namespace definition into the class and it would then work like I'm used to with C#, but that's not the case. If I added the same namespace that's in my C# class (using Namespace MvcMusicStore.Controllers) it would be still be prefixed by the project's default namespace, so I'd end up with my HomeController class in MvcMusicStore.MvcMusicStore.Controllers.
In the end, I decided it was best to follow the When In Rome approach and fit with how the standard ASP.NET MVC VB project templates work. Looking back on my first steps from VB into C#, I remember being a little confused and frustrated by the implied requirement to drop namespaces everywhere, and I didn't even really reconsider it until I read Jeff Atwood's A Modest Namespace Proposal post asking why we're all slavishly adding unnecessary namespaces in cases when they add no value.
Note: Many posts that compare or even mention both C# and VB bring out the crazies. Be warned that I will probably edit any "my language is better that your language" comments, replacing bile with lyrics from contemporary popular music.