Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / C#

Simple Source Line Counter in C# for C#

Rate me:
Please Sign up or sign in to vote.
4.25/5 (28 votes)
24 Jan 20053 min read 102.4K   3.4K   28  
A simple source code line counter written in C#.
using System;

namespace CSharpLineCounter
{
	public class Simpleton: Brain
	{
		#region Fields
		private bool _CountBlankLines;
		private bool _CountRegions;
		private bool _CountMultiLineComments;
		private bool _CountSingleLineComments;
		private bool _CountXMLSummaries;

		private bool _TrackingComment;
		#endregion

		#region IsSource
		public override bool IsSource(string aLine)
		{
			aLine = aLine.Replace("\t", String.Empty);

			if (_TrackingComment)
			{
				if (aLine.IndexOf(@"*/") != -1)
					_TrackingComment = false;

				return _CountMultiLineComments;
			}

			else if (aLine.Length == 0)
				return _CountBlankLines;

			else if (aLine.StartsWith(@"///"))
				return _CountXMLSummaries;

			else if (aLine.StartsWith(@"//"))
				return _CountSingleLineComments;

			else if (aLine.StartsWith("#region") || aLine.StartsWith("#endregion"))
				return _CountRegions;

			else if (aLine.StartsWith(@"/*"))
			{
				_TrackingComment = true;

				return _CountMultiLineComments;
			}

			else if (aLine.IndexOf(@"/*") != -1)
			{
				_TrackingComment = true;

				return true;
			}

			else
				return true;
		}
		#endregion
		#region Clear
		public override void Clear()
		{
			_TrackingComment = false;
		}
		#endregion

		#region CountBlankLines
		public bool CountBlankLines
		{
			get
			{
				return _CountBlankLines;
			}
			set
			{
				_CountBlankLines = value;
			}
		}
		#endregion
		#region CountMultiLineComments
		public bool CountMultiLineComments
		{
			get
			{
				return _CountMultiLineComments;
			}
			set
			{
				_CountMultiLineComments = value;
			}
		}
		#endregion
		#region CountRegions
		public bool CountRegions
		{
			get
			{
				return _CountRegions;
			}
			set
			{
				_CountRegions = value;
			}
		}
		#endregion
		#region CountSingleLineComments
		public bool CountSingleLineComments
		{
			get
			{
				return _CountSingleLineComments;
			}
			set
			{
				_CountSingleLineComments = value;
			}
		}
		#endregion
		#region CountXMLSummaries
		public bool CountXMLSummaries
		{
			get
			{
				return _CountXMLSummaries;
			}
			set
			{
				_CountXMLSummaries = value;
			}
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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 tom@sagerion.com, I would love to hear about your next ground-breaking project.

Comments and Discussions