Does C# Support Project-Wide Default Namespace Imports Like Vb.Net

Does C# Support Project-Wide Default Namespace Imports Like VB.NET?

With C# 10 this answer has changed.

C# 10 introduces [Global using directives][1]:

Global using directives
You can add the global modifier to any using directive to instruct the compiler that the directive applies to all source files in the compilation. This is typically all source files in a project.

importing namespace and using their hierarchy

VB.Net projects allow you to specify project-wide namespace imports. These can be seen on the References page of your project's propertes. It's likely that your first project, P1, has a project-wide import of System.Data.SqlClient whereas P2 does not.

Apply an added reference to all windows forms

Here's one approach:

  • Edit -> Find And Replace -> Find in Files
  • in Find what box write: using System;
  • in Replace with box write: using System;\r\nusing mysql.data;
  • in Find options group check use regular expressions and in Look at these file types add *.cs

c# where to place using for every page?

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

Does importing more namespaces have an effect on performance?

I guess the by import you refer to the using directive in C#.

No - it does not.

The import only affect the compiler parsing process.

It just tells the compiler the full name of the classes in the file.

It is equivalent to a fully qualified class names instead of import.



Related Topics



Leave a reply



Submit