C# Code Style using EditorConfig

Jonathan Harrison
2 min readMay 1, 2018

--

I am currently in the middle of creating a Xamarin app for Android and iOS, which I will talk about more once I near completion. I just thought I would share something I discovered when setting up the project.

I was looking into which Code Quality tools to use for C#/Xamarin. On previous work projects I had used Code Analysis and StyleCop; but I wanted to investigate other options and really not go overboard i.e. trying to keep StyleCop errors at bay for a one man project.

On my way home from work I was listening to an episode of the .NET Rocks! podcast (I highly recommend it), where they were discussing productivity tools. One thing that stood out was that Visual Studio 2017’s Code Style preferences (which I was already using to remind me of new C# features) supported being specified via EditorConfig. This meant that these settings could live alongside my code in Source Control and travel with it.

After some playing around, I finalized an EditorConfig that suited my preferences, and I thought would make it available as an example for anyone else interested.

Example .editorconfig

Code Style preferences come in three categories:

  • Language conventions
  • Formatting conventions
  • Naming conventions

I have specified all the Language and Formatting conventions currently available, and included some Naming conventions which could be expanded to include rules likes private fields must start with _.

Example .editorconfig

The one thing to note though is that these rules aren’t enforced by Continuous Integration builds, so other tools like Roslyn Analyzers need to be used to perform build time checks. One example is SonarAnalyzer.

--

--

Jonathan Harrison

Principal Software Engineer @ Tripadvisor. Formerly at Spotify, Altitude Angel and Microsoft. All opinions are my own.