Okay, there’s System and System.Web. Am I correct in that the structure this suggests is:
namespace System
{
// all of the outer namespace members
namespace Web
{
// all of the inner members
}
}
And that when a namespace is nested within another, having a using directive with the parent/outer namespace only doesn’t automatically bring in the child/nested namespace? In other words:
using System;
public class Example
{
public Example()
{
context1 = new HttpContext(); // won't work
context2 = new System.Web.HttpContext(); // will work
}
}
Just trying to see if I actually understand this correctly.
You could nest namespaces and any
usingdirective would only grant access to the members defined within the specific namespace you are referencing.So from your example:
Referencing
Systemwould grant you access to the outer namespace members and referencingSystem.Webwould grant you access to all of the inner namespace members.But this is atypical and usually namespaces are defined only once within a file. The dot-notation typically follows a folder or project structure, so files that were nested as such:
WebApplication - Models - MyModel.cs - Controllers - MyController.csMight use namespaces of
WebApplication.ModelsandWebApplication.Controllers.I can’t think of a great example off the top of my head where you would want to nest namespaces, but there may be a good reason to. However, it would be considered an exception to the rule, in my opinion.