Home » C# » How to count the lines of Code in a Visual Studio solution

How to count the lines of Code in a Visual Studio solution

By Emily

I’m not saying that lines of code is an appropriate way of measuring how much work someone’s done, but it can be nice to see just how much work you’ve produced when you’ve had your head down coding for a while. I needed to find out how many lines of code there were in my Visual Studio solution including both the main project, and the unit test project. Note that the solution described in the post is available from Visual Studio 2019 version 16.4 and Microsoft.CodeAnalysis.Metrics (2.9.5).

How to find how many lines of code in Visual Studio project

To count the lines of code in Visual Studio 2019 you need to :

  1. Right click on the Solution
  2. Click on the Analyze and Code Cleanup menu option
  3. Click on the Calculate Code Metrics menu option
how to count lines of code in a visual studio 2019 solution

Seeing the lines of code counts

Once you’ve followed the steps described above you should see a panel appear which will contain the line counts not only for the overall solution but for each project in the solution too.

how to see lines code count in visual studio

More about Code Metrics

Now you’ve seen how easy it is see the, ‘lines of code’ counts it’s worth digging a bit deeper into the Code Metrics functionality that Visual Studio offers. The metrics available are:

  1. Maintainability Index – the relative ease of maintaining the code, a high value is good.
  2. Cyclomatic Complexity – the structural complexity of the code.
  3. Depth of Inheritance – indicates the number of classes that inherit from one another, a low value is good.
  4. Class Coupling – a high coupling indicates a design that is difficult to reuse and maintain because of its many interdependencies on other types.
  5. Lines of Source Code – this is the literal count of lines, including blank lines.
  6. Lines of Executable Code – this is the count of real lines of executable code.

Show the line numbers in Visual Studio

To show the line numbers in Visual Studio follow these steps.

  1. Click the Tools menu
  2. Click the Options button
  3. In the left hand pane select All languages, or a specific language. In the right hand pane check the line numbers checkbox
How to show line numbers in Visual Studio

You may also find these interesting: