Home arrow C# arrow Page 6 - C# - An Introduction

C# - An Introduction

Learn the fundamentals of C# as Michael takes us on an introductory tour, including keywords, identifiers, and much more.

Author Info:
By: Michael Youssef
Rating: 4 stars4 stars4 stars4 stars4 stars / 310
November 17, 2003
  1. · C# - An Introduction
  2. · Keywords
  3. · Identifiers
  4. · C# Statements
  5. · C# Building-Blocks
  6. · Commenting Your Code
  7. · Case-Sensitivity and Syntax Errors
  8. · Organizing Code Using White Space
  9. · Conclusion

print this article

C# - An Introduction - Commenting Your Code
(Page 6 of 9 )

Just like any other language, when writing C# code you can also add comments. A comment is simply a section of text that you write to explain or describe your code.  For example, you have some block of code that adds two numbers, so you use comments to describe the operation of this block of code like the following:

    // This block of code will add 2 numbers and then put
   // the result in the memory
    int memory = 2 + 5;

Another feature of VS.Net is coloring the comments green.  You can write your comments using any language (English, French, etc); the C# compiler will simply ignore them.  You can think of comments as something that can live with your C# code.  In more complex blocks of code you will find that comments are very useful, especially if you are reading other programmers’ code.   Their comments will help you understand many things.

There are two ways to write comments in C#. The first way is using multi-line comments; the second, using single-line comments.

Multi-line comments begin with the characters “/*” and end with the characters “*/” as follows:

/* this block of code will add 2 numbers
* and then put the result in the memory */

There are a few things that we must discuss here. This is a multi-line comment so you can press the Enter key after the opening characters “/*” and each time you press the enter key (for a new line) you will get a “*” character at the beginning of each new line.  The job of this character is to tell you that you have a new line in your comments.  You can delete this character and continue writing your comments.  When finished, you must close it with “*/” characters.  When you close your multi-line comments, VS.Net will bold all the text contained in your comments so that you may see the entire block of comments you have just written.

In multi-line comments you can write anything except the closing character “*/” , asVS.Net will think that you want to close the comments.

So you can’t write the following:

/* the only special character that you can't write
* Inside the multi-line comments is the "*/" character */

Here, we wrote the closing characters as part of our multi-line comment.   Note that after the closing characters in the second line “*/” the text color is black, meaning it is no longer recognized as a comment.

A single-line comment begins with the characters “//”.  You can write your comments only on this single line as follows:
// This block of code will add 2 numbers and then put
int memory = 2 + 5;

If you want to write more than one line in your comment then you must begin each line with “//” characters as follows:
// This block of code will add 2 numbers and then put
// the result in the memory
int memory = 2 + 5;

You can write the single-line comment in the same line with your statement like so:

int memory = 2 + 5; // this is a statement

There is a third way to comment your code; however, I prefer to not discuss it now.  Rather, I will show you what the third method looks like: 

/// <summary>
/// The main entry point for the application.
/// </summary>

You will find this kind of commenting in C# projects created by VS.Net and it helps you to document your application.

Documentation is a help system developers create so that the application’s users can learn how to make use of the application.  Later on, I will give you an overview of how to create documentation for your application using this commenting system.

Some developers have said that this is not a comment because you don’t comment your code; rather you describe it.  Nevertheless, we will discuss it later.

blog comments powered by Disqus

- Introduction to Objects and Classes in C#, P...
- Visual C#.NET, Part 1: Introduction to Progr...
- C# - An Introduction
- Hotmail Exposed: Access Hotmail using C#
- Razor Sharp C#
- Introduction to Objects and Classes in C#
- Making Your Code CLS Compliant
- Programming with MySQL and .NET Technologies
- Socket Programming in C# - Part II
- Socket Programming in C# - Part I
- Creational Patterns in C#
- Type Conversions
- Creating Custom Delegates and Events in C#
- Inheritance and Polymorphism
- Understanding Properties in C#

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials