πŸ“’
SDV503
  • SDV503
  • Command Prompt
    • Windows Command Prompt
    • Mac OS Terminal
  • GIT & GITHub
    • GitHub
    • Git
      • Git Workflow
        • Forking Workflow
  • README
    • How to write a readme.md file?
    • Write a better README.me
    • Generate a README for GitHub Repo Quickly
  • Code-comments
    • Clean Comments
    • Writing Comments in a Good way
  • Pair Coding
    • What is Pair Coding?
    • Pair Programming Experience
  • Programming?
    • What is Programming?
    • What Is A Programming Paradigm?
    • JavaScript Programming Paradigms
  • Number Systems
    • Decimal and Binary numbers
  • JavaSCript
    • Introduction To JavaScript
      • The JavaScript Engine
  • JS Call Stack
    • JavaScript call stack
    • JavaScript & Memory
      • Memory Leaks in JavaScript
    • Execution Context and Execution Stack in Javascript
      • Javascript Execution Context and Hoisting
  • JavaScript Variables
    • Introduction to JS Data Types
    • Primitive and Non-Primitive
    • Operator precedence and associativity
      • JS Operators Part One
      • JS Operators Part Two
      • JS Operators Part Three
    • Clean Code for JS Variables
  • JavaScript Scopes
    • Scope (Chain) Visualized
  • Javascript β€Šβ€”β€Šthis keyword
  • JavaScript Data Types
    • More JavaScript Data Types
  • JavaScript Expressions and Statements
  • if/else, switch and ternary operator
    • If/Else statement
  • Switch Statement
  • Ternary Operator
  • JavaScript Loops
    • Loops in JavaScript
      • Trouble With Loops
  • Objects
    • JavaScript Objects
      • Prototypal Inheritance Visualized
      • JavaScript Number Object
      • JavaScript String Object
  • Functions
    • JavaScript Function Part One
    • JavaScript Function Part Two
    • Immediately Invoked Function Expressions ~ IIFE
    • JS => Arrow Functions
    • JavaScript Callback
    • Hoisting in JavaScript
      • Hoisting Visualized
    • Recursion Functions
    • Curry and Function Composition
  • JavaScript Features
    • JSpread Operator
    • JavaScript Built-in Functions & Objects
  • Data Structures&Algorithms
    • JavaScript’s Data Types
    • Data Structures in JavaScript
      • Introduction to Big O Notation
      • Big O Notation in Javascript
      • Linked Lists
        • Linked Lists β€” 2
      • Hash Tables
      • Stack & Queue
  • TLDR
    • Single quotes (β€˜ ’) and double quotes (β€œ ”) in JavaScript
  • ES6
    • Generators and Iterators
    • Javascript Classes
    • JavaScript closures
    • JavaScript Promises & Async/Await
      • Event Loop Visualized
  • C#
    • What does C#? (C Sharp)
    • C# vs JavaScript
    • What Is The Difference Between C#, .NET, ASP.NET, Microsoft.NET, and Visual Studio?
    • What is the .NET Framework?
    • Methods and Properties of Console Class in C#
    • Datatypes in C#
    • C# Code Comments
    • The if statement
    • The switch statement
    • Loops
    • Comparison operators
    • Addition assignment operators
    • The String Interpolation Operator
    • Arrays
    • Lists
    • Dictionaries
Powered by GitBook
On this page
  • Single-line comments
  • Multi-line comments
  • Documentation comments
  • Code comments & the Task list
  • Summary

Was this helpful?

  1. C#

C# Code Comments

When writing code, you will quickly get used to the fact that pretty much any character or word you enter will have a special meaning. For instance, you will see a lot of keywords in C#, like class, namespace, public and many more. You will also see that the compiler makes sure that you are using these keywords, as well as your own variables and methods, in the correct way. C# is a fairly strict language and the compiler will help you make sure that everything is entered the way it should be. However, you do have a single possibility to write whatever you like, thanks to the concept of code comments.

You may have already experienced comments in some of the code you have seen, be it C# or any other programming language - the concept of comments in code is pretty universal. The way they are written varies a lot though, so let's have a look at the type of comments you can use in your C# code.

Single-line comments

The most basic type of comment in C# is the single-line comment. As the name indicates, it turns a single line into a comment - let's see how it might look:

// My comments about the class name could go here...
class Program
{
    ......

So that's it: Prefix your lines with two forward slashes (//) and your text goes from something the compiler will check and complain about, to something the compiler completely ignores. And while this only applies to the prefixed line, you are free to do the same on the next line, essentially using single-line comments to make multiple comment lines:

// My comments about the class name could go here...
// Add as many lines as you would like
// ...Seriously!
class Program
{
    ...... 

Multi-line comments

In case you want to write multiple lines of comments, it might make more sense to use the multi-line comment variant that C# offers. Instead of having to prefix every line, you just enter a start and stop character sequence - everything in between is treated as comments:

/*  
My comments about the class name could go here...  
Add as many lines of comments as you want  
    ...and use indentation, if you want to!  
*/  
class Program  
{
    ......

Use the start sequence of forward-slash-asterisk (/*), write whatever you like, across multiple lines or not, and then end it all with the end sequence of asterisk-forward-slash (*/). In between these markers, you can write whatever you want.

As with pretty much any other programming-related subject, whether to use multiple single-line comments or one multi-line comment is often debated. Personally, I use both, for various situations - in the end, it's all up to you!

Documentation comments

Documentation Comments (sometimes referred to as XML Documentation Comments) look like regular comments, but with embedded XML. Just like with regular comments, they come in two forms: Single-line and multi-line. You also write them the same way, but with an extra character. So, single-line XML Documentation Comments uses three forward slashes (///) instead of two, and the multi-line variant gets an extra asterisk added in the start delimiter. Let's see how it looks:

class User
{
    /// 
    /// The Name of the User.
    /// 
    public string Name { get; set; }    /**
    * The Age of the User.
    */
    public string Age { get; set; }
}

Here you can see both variants - single-line and multi-line. The result is the same, but the first variant tends to be the most commonly used for documentation comments.

Documenting your types and their members with documentation comments is a pretty big subject, and therefore it will be covered more in-depth in a later article, but now you know how they look!

Code comments & the Task list

If you're using Visual Studio, you can actually get some help tracking your code comments. In the Task List window (access it from the menu View > Task List) your comments will appear if they use the special, but very simple Task List comment syntax:

//TODO: Change "world" to "universe"
Console.WriteLine("Hello, world!");
//HACK: Don't try this at home....
int answerToLife = 42;

So if the single-line comment is immediately followed by TODO or HACK, it will appear in the Task List of Visual Studio, like this:

And there are more types - depending on the version of Visual Studio you're using, it will respond to some or all of the following comment tokens:

  • TODO

  • HACK

  • NOTE

  • UNDONE

Summary

Code comments are extremely useful in documenting your code or for the purpose of leaving clues to yourself or your potential colleagues on how stuff works. As an added benefit, they're great when you need to test something quickly - just copy a line and comment out the original line and you can see how it works now. If you're not happy with the result, you can just delete the new line and uncomment the original line and you're back to where you started.

And don't worry about the end-user snooping through your comments - they are, as already mentioned, completely ignored by the compiler and therefore not in any way included in your final DLL or EXE file. Code comments are your personal free space when programming, so use them in any way you want to.

PreviousDatatypes in C#NextThe if statement

Last updated 4 years ago

Was this helpful?