Comments

Lesson 23
Author : Afrixi
Last Updated : February, 2023
C# - Programming Language
This course covers the basics of programming in C#.

In programming, comments are lines of text in the code that are not executed by the computer. They are used to explain the purpose of the code, add documentation, or temporarily disable code for testing purposes.

In C#, there are two types of comments:

Single-line comments: These are comments that begin with two forward slashes (//). They are used to add comments on a single line. Example:

// This is a single-line comment
int x = 5; // This is also a single-line comment

Multi-line comments: These are comments that begin with a forward slash and asterisk (/*) and end with an asterisk and forward slash (*/). They are used to add comments that span multiple lines. Example:

/*
This is a
multi-line
comment
*/
int x = 5; /* This is also a multi-line comment */

It’s important to add comments to your code to make it more understandable and maintainable. It can also help others who may read and work with your code in the future.

Here are some tips for writing good comments:

  • Be concise: Comments should be short and to the point. Avoid writing long paragraphs or overly detailed explanations.
  • Be clear: Make sure your comments are easy to understand and don’t leave room for confusion.
  • Be consistent: Use the same style and formatting for your comments throughout your code.
  • Avoid redundant comments: Don’t repeat what is already obvious from the code.
  • Update comments: Remember to update your comments if you make changes to the code.