Home Page
"Join my free Amazing ASP.NET newsletter"
It's safe and easy to signup. You get:
  • Notified anytime a new article comes out
  • Technical step-by-step tutorials on all important ASP.NET topics
  • Expert guidance so you can reach unlimited ASP.NET success
  • Important ASP.NET news updates
Enter your Email

In C#, comments are blocks of code that are marked with the characters “//” and are ignored by the compiler.  Comments are used to either put documentation into your program or make some lines of code inactive.  For example you may want to add a comment above some of your source code that describes specifically what that part of the program does.  This is very useful even for yourself so that when you come back to your source code later, you can quickly remember why or how you programmed something.  Another example of comment use is to mark some of your lines of code as inactive so that the compiler ignores them.

To create comments you have two options.  You can type “//” on a blank line in the code editor and then type whatever text you want following that to complete the comment.  You can also highlight a block of source code and push the comment button on the toolbar at the top of Visual Web Developer.  The comment button tooltip says “Comment out the selected lines”.  There is also an uncomment button next to it which does the reverse and removes the comment characters from a block of code.  When you use the comment button in the toolbar, all it does is add the “//” characters to the beginning of each line of code that you have highlighted.  Click here to watch a sample video where I add comments to my program using both techniques.  Notice that I also use the uncomment button.

Here are some tips and techniques for using comments:

  • A very common way to use comments is to place them above variable declarations.  We will see examples of this usage of comments in a later lesson entitled “Data types lab exercise”.
  • When you are coding a complicated program and you need to compile the application, but there are sections that you are unsure about.  You can comment those sections out temporarily and perform a compile.  This is especially handy if you have an idea in your head and you want to start programming it, but you don’t have time to finish it so you temporarily comment it out so that you don’t lose the idea.
  • If you are programming a module and you are referring to an example from a book or website, you can paste the example code into your program and comment it out so that the compiler can ignore it.  I use this technique a lot and I recommend that beginning programmers do that as well.  Sometimes I permanently leave the reference material as comments in my program just so that I can keep track historically of where I got it from. 

4 Comments »

  1. Ted,

    Thanks for sharing your extensive knowledge. I have some experience with VBA for Excel and have fairly advanced skills with Oracle but was having a hard time finding an insertion point to C#, VWeb and SQL Server. Every attempt to learn has made me painfully aware of pieces of perequisite knowledge that I was missing. Your lessons are very well laid out in that they walk the student logically through the learning process, building as they go. The only recommendation I might make would be for you to add navigation buttons/links to next or previous topic. Otherwise I am very pleased to have found this site and am now working my way through the lessons. Great stuff! Thanks again!

    Jay

    Comment by Jay — February 1, 2009 @ 5:14 pm

  2. In the express edition you need to activate the Text edit toolbar:

    view/toolbars/Text Edit

    Comment by Rene van Putten — July 8, 2009 @ 9:13 am

  3. I’m having a heck of a time with the Visual Web Dev 2008 express, specifically in regards to code commenting. It seems no matter what I do, comments are only entered as html comments, regardless of whether i’m in an asp/vb block or a js block. Now, the lack of context-aware commenting wouldn’t be so bad if there was some way to control comment formatting, but if there is, I can’t figure it out. I’d be happy if I could just set the default to use vb style comments as that’s what i’m usually doing in the editor. Any help appreciated!

    Comment by mike — July 22, 2009 @ 7:18 pm

  4. thanks you so so much…….

    Comment by Junaid — July 27, 2009 @ 5:13 am

RSS feed for comments on this post. TrackBack URL

Leave a comment