Follow us

.NET source code analysis

Static code analysis during your development with visual studio. It help you to provide suggestions, error, warning on code build and suggestion while writing code.

It also help you to auto formatting code based on your setting. Just write your code and then press (CTRL + K + D) and it will do formatting of your code.

 

To enable this for your solution: Go to your solution properties > code analysis tab and enable

  • "Enable .NET analyzer" - this option will enforce code analysis for your solution and will work as per your code analysis configuration setting.
  • "Enforce CodeStyle on build" - this option will enforce raising error/warning on your code build as per your code analysis configuration setting. If you do not enable this option, then when you build your code (CTRL + SHIFT + B) then it will show output as build successful but into error/warning tab you will see the respective error and warning.

 

 

When you enable above option, it add respective attribute into your project file.

 

Now, to configure setting how you want to analyze your code and do auto formatting: To do this, add new item "EditorConfig" on your solution level:

 

Once you add "EditorConfig" file, it generate ".editorconfig" file inside your solution with default code analysis settings and there you can add/update setting as per your need. Here specially I have added/updated few setting for my testing to show you. (refer yellow highlighted one).

Additional Note:

While we add any rule to editor config file, there are multiple different options how we want to act on rule? Below are some right side action value:

  • =none
  • =suggestion
  • =warning
  • =error

Example like : dotnet_naming_rule.types_should_be_pascal_case.severity = none

 

  • =true:suggestion
  • =true:warning
  • =true:error

Example like : dotnet_style_readonly_field = true:warning

 

 

Now, here is my code and when I build(CTRL + SHIFT + B) my code, it throw error as per my setting I had done ".editorconfig" file.

 

 

Similar to ".editorconfig" file setting option, we have global setting file also which apply globally:

If you name these files as .globalconfig then they will be implicitly applied to all the C# and Visual Basic projects within the current folder, including subfolders. Otherwise, you must explicitly add the GlobalAnalyzerConfigFiles item to your MSBuild project file:

 

<ItemGroup>

  <GlobalAnalyzerConfigFiles Include="<path_to_global_analyzer_config>" />

</ItemGroup>

 

 

Main doc reference: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files

EditorConfig code style rule available options: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options

 

Categories/Tags: code analysis~auto code formatting

Recent Articles

1

AWS Saving Plan - Cost optimization tips

2
3

AWS RDS Key Concepts & Why you should use it?

4
5

Open-Search/Kibana - Multi Tenancy Setup

See All Articles