If I have a
using MyProject.language;
literary needs to place on every single page.
My question is, is there a place that I can place the "using" just once, for every page of the project.
Thanks.
If I have a
using MyProject.language;
literary needs to place on every single page.
My question is, is there a place that I can place the "using" just once, for every page of the project.
Thanks.
Using statements have to be in the file where you are actually using the namespace. There is no such thing as a one-place-for-all-usings in C#, unlike C++. The only 'exception' to this are ASP / ASP-MVC files, which automatically add all the using statements defined in the web.config namespaces
node. C# / .cs files do not make use of this facility however and still need every namespace manually defined.
Since you mentioned page, you can configure ASP.NET to reference the namespace everywhere. This only affect the views themselves and not the code behind
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="Microsoft.Web.Mvc" />
<add namespace="xVal.Html" />
<add namespace="Telerik.Web.Mvc.UI" />
</namespaces>
</pages>
If you use the Razor view engine of ASP.NET MVC, you can add a reference for all views like this
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
If you are in a plain C# project or in the code behind of a page, you should know that any class that is located in a namespace will automatically reference all object in any parent namespace. Assuming a namespace Company.Software.Somethings.Data
, any classes in the Data
namespace will automatically reference the classes in
Company.Software.Somethings
Company.Software
Company