<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nitin Manju</title>
    <description>The latest articles on DEV Community by Nitin Manju (@nitinmanju).</description>
    <link>https://dev.to/nitinmanju</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F924281%2Fd4017be2-ae15-4c1f-9119-91be55c4f6b7.jpeg</url>
      <title>DEV Community: Nitin Manju</title>
      <link>https://dev.to/nitinmanju</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nitinmanju"/>
    <language>en</language>
    <item>
      <title>De-clutter namespaces in C# 10</title>
      <dc:creator>Nitin Manju</dc:creator>
      <pubDate>Sun, 11 Sep 2022 07:40:58 +0000</pubDate>
      <link>https://dev.to/nitinmanju/de-clutter-namespaces-in-c-10-2f1p</link>
      <guid>https://dev.to/nitinmanju/de-clutter-namespaces-in-c-10-2f1p</guid>
      <description>&lt;p&gt;C# 10 was released last year and it has introduced a host of new features. Below is a list of useful features specifically designed around the declarations of namespace that can help you tidy your code base.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o8NoEuzh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m60a7vraqik4j3qclsg9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o8NoEuzh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m60a7vraqik4j3qclsg9.png" alt="Photo by Mohammad Rahmani on Unsplash" width="880" height="592"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Global namespaces
&lt;/h2&gt;

&lt;p&gt;If you have a large code base, chances are, the file is already cluttered with using statements. C# 10 introduces global namespaces, which like the name suggests, can be declared globally and will be available across all the files in your project. The global modifier is used to achieve this as shown below:&lt;/p&gt;

&lt;p&gt;global using ;&lt;/p&gt;

&lt;p&gt;example: &lt;code&gt;global using System.Configuration;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Typically, you would declare all the global namespaces in a single file like GlobalUsings.cs and that should allow you to use the namespaces in each file within the project without explicitly writing the using statements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implicit namespaces
&lt;/h2&gt;

&lt;p&gt;C# 10 allows you to clean up ‘using ’ statements even further with implicit namespaces. When enabled, you don’t have to write the &lt;code&gt;‘using &amp;lt;namespace&amp;gt;’&lt;/code&gt; statement for commonly used namespaces like System, &lt;code&gt;System.IO&lt;/code&gt; etc as they will be referred implicitly in all the files within the project.&lt;/p&gt;

&lt;p&gt;When combined with global namespaces, it will further reduce the number of &lt;code&gt;‘using &amp;lt;namespace&amp;gt;’&lt;/code&gt; statements required in a C# file since the commonly used namespaces don’t have to be declared globally. However it is not mandatory to combine them together.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rHYphVIB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/loe0cdwhar833b99d2a0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rHYphVIB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/loe0cdwhar833b99d2a0.png" alt="Implicit namespaces" width="797" height="513"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This feature should be enabled by default for projects created using Visual Studio 2022 and .NET 6. However, to achieve this manually add the property &lt;code&gt;ImplicitUsings&lt;/code&gt; and set it to enable in the project properties as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;PropertyGroup&amp;gt;
    &amp;lt;!-- Other properties like OutputType and TargetFramework --&amp;gt;
    &amp;lt;ImplicitUsings&amp;gt;enable&amp;lt;/ImplicitUsings&amp;gt;
&amp;lt;/PropertyGroup&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  File-scoped namespaces
&lt;/h2&gt;

&lt;p&gt;C# 10 allows you to create namespaces inside a file in a declarative manner. A namespace declaration can simply be written as below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XbJJMg_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhppn7ez3v9t0d6w8rt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XbJJMg_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhppn7ez3v9t0d6w8rt7.png" alt="File-scoped namespaces" width="752" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice the ‘;’ after the namespace and the missing curly bracket enclosure. The code inside this file will be part of the declared namespace and this new namespace can be referred in other C# files or globally.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Sync Namespaces
&lt;/h2&gt;

&lt;p&gt;This is a new Visual Studio 2022 feature and is not tied with C# 10.&lt;/p&gt;

&lt;p&gt;Sync Namespaces should work on earlier versions of .NET and C#. Right Click on the Solution or Project File and you should find it as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d_-M594r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pazxwqhnn4rw4lvti8wd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d_-M594r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pazxwqhnn4rw4lvti8wd.png" alt="Sync namespaces menu in Visual Studio" width="507" height="713"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have C# files which have been moved between folders and the namespaces are out of sync, this feature should come in handy to set the right namespace for each file based on the . format.&lt;/p&gt;

&lt;p&gt;This will specially come in handy when you are performing a migration of a legacy code-base.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>visualstudio</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
