Click here to Skip to main content
13,763,800 members
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 24 Jan 2005

Simple Source Line Counter in C# for C#

, 24 Jan 2005
Rate this:
Please Sign up or sign in to vote.
A simple source code line counter written in C#.

A Simple C# Line Counter

What is it?

A stand-alone utility that counts the lines of code in C# source files.

My searches on CP for source code line counters yielded some nice alternatives. However, I wanted something that was written in C# and did not install into Visual Studio. The only "integration" I was looking for was of the manual "External Tools" variety. I did not find a line counter that met these criteria, so I coded one up.


Simply unzip and run the executable. Select the directory where the source files reside (all subdirectories will be searched). Click the "Go" button to start the counting process. Only files with a ".cs" extension will be considered.

The goal for this counter was simplicity. In other words, just count the lines of code in the project. I didn't care about comment-to-source ratios or how much white space had been used - after all, we already know our source is pristine, right? We just need to find out how much of this wonderful stuff we've got. ;) In the pursuit of not driving myself totally nuts with this thing, I did put in two features. One, the application remembers the last directory selected (if it still exists). And two, the size and location of the form is restored (if the SystemInformation.WorkingArea is large enough to accommodate the previous settings).

What exactly is a line of code?

I'm glad you asked. Remember, this counter is simple. If a source code line trims down to nothing, it doesn't count. If, after being trimmed down, a line starts with #region, #endregion, or //, then it doesn't count. Lines which start with source code and end with a comment do count. For example, if a /* style comment starts halfway through a line, that line is counted. All subsequent lines up to the end comment */ are not counted. XML style comments also do not count. Now, can you defeat these little schemes and get some of your comments counted? Sure. In fact, you can count every line in the file as source if you like. What better way to get your LOC rating up to 5,000 a day, eh? In order to change the line counting behavior, we simply replace the brain of the counter.

Brain Surgery

Let's not get all stilted here and call it a parser, String.IndexOf(); and String.StartsWith(); do not make a parser. However, the counter definitely has to do something (if only a little something) to figure out whether or not a given line is really source. This all important function is the responsibility of the Brain class. In the constructor of the LineCounter class, you'll find this bit of code:

CodeFileModel _Model = new CodeFileModel();

Brain simpleton = new Simpleton();

_Model.Brain = simpleton;

Now the implementation of Simpleton, is, well, simple. He enforces all of the rules outlined above with slavish devotion. We can change his behavior though, by setting Boolean properties that represent the type of line we want to include in the count:

simpleton.CountBlankLines = true;
simpleton.CountRegions = true;
simpleton.CountSingleLineComments = true;
simpleton.CountMultiLineComments = true;
simpleton.CountXMLSummaries = true;

With this configuration, Simpleton would count all of the lines as source. Alternatively, you could implement a whole new Brain descendant. Brain contains only one function IsSource which takes a single line as a parameter and returns a Boolean indicating whether or not the line should be counted.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Tom Ollar
CEO Sagerion, LLC
United States United States
I read About Face by Alan Cooper in 1995 and immediately recognized it as a founding document for the future of software. I also recognized we had a long, long way to go - and yes, even with the advent of iOS, we are still not there yet.

At my company, Sagerion (say-jair-ee-on), we can take a look at your planned or existing software and suggest ways of making it better - lots better. We can develop down-to-the-pixel blueprints showing exactly what our suggestions mean. We can help manage on-going development to make sure the top-notch user-experience we've suggested really does get built. Now, honestly, how often have you ever seen all those things happen?

You may or may not already have great development going on - but what does that matter if you don't have great design driving it?

Feel free to contact me at, I would love to hear about your next ground-breaking project.

You may also be interested in...

Comments and Discussions

QuestionSmall bug in project Pin
Johniak8-Apr-12 23:43
memberJohniak8-Apr-12 23:43 
GeneralIt hangs on "select folder" Pin
mtthwbrnd1-Apr-11 1:03
membermtthwbrnd1-Apr-11 1:03 
GeneralRe: It hangs on "select folder" Pin
tomollar5-Apr-11 3:04
membertomollar5-Apr-11 3:04 
GeneralCome, it's just a simple work, but make it complicated Pin
SVGamer14-Oct-10 10:40
memberSVGamer14-Oct-10 10:40 
GeneralMy vote of 2 Pin
kuratowski11-Mar-10 3:59
memberkuratowski11-Mar-10 3:59 
GeneralCode Downloaded and compiled - but Folder Dialog does not work Pin
mitchellm4411-Mar-08 5:27
membermitchellm4411-Mar-08 5:27 
GeneralRe: Code Downloaded and compiled - but Folder Dialog does not work Pin
Jonathan C Dickinson28-May-08 1:54
memberJonathan C Dickinson28-May-08 1:54 
GeneralA small bug fix Pin
Senaka Fernando22-Jun-07 8:50
memberSenaka Fernando22-Jun-07 8:50 
QuestionBlix ??? Pin
Tony Tam1-Jan-07 20:22
memberTony Tam1-Jan-07 20:22 
AnswerRe: Blix ??? Pin
espeholt_jr24-Mar-07 13:16
memberespeholt_jr24-Mar-07 13:16 
GeneralRe: Blix ??? Pin
the_hijacker6-May-11 2:15
memberthe_hijacker6-May-11 2:15 
GeneralVS2005 Pin
NZ Programmer10-Sep-06 3:27
memberNZ Programmer10-Sep-06 3:27 
GeneralGreat Pin
Piotr Nogalski29-Jan-06 23:57
memberPiotr Nogalski29-Jan-06 23:57 
GeneralException Pin
Member 58840125-Jul-05 10:23
memberMember 58840125-Jul-05 10:23 
GeneralFeedback Welcome! Pin
Tom Ollar24-Jan-05 7:23
memberTom Ollar24-Jan-05 7:23 
GeneralRe: Feedback Welcome! Pin
Uwe Keim24-Jan-05 20:07
sitebuilderUwe Keim24-Jan-05 20:07 
GeneralRe: Feedback Welcome! Pin
Tom Ollar25-Jan-05 4:48
memberTom Ollar25-Jan-05 4:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181113.4 | Last Updated 24 Jan 2005
Article Copyright 2005 by Tom Ollar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid