TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Comment: Single Line and Multiline

Use comment syntax and if-false regions to describe code. Comments do not affect runtime speed.
Comments. The C# language supports 2 comment syntax forms. Comments do not affect a program's speed. Directives can be used as comments.
Notes, compilers. Some programming languages have been affected in some way by comments in the program text. But C# is a compiled language—comments do not affect the executable.
Example comments. Here are some comments. We see the C++ style single-line comments. We see the multiline comment syntax used in C programs.

Directives: For long blocks of commented text, a directive like #if-false can be used.

C# program that uses comments using System; class Program { static void Main() { // An example comment. // ... Sometimes indenting comments is helpful too. int value = 10; // Ten. /* A multi-line comment. */ value++; #if false Another way to comment, an #if false directive. #endif value *= 100; Console.WriteLine(value); } } Output 1100
XML comments. These are part of the C# language specification, but not C# code. The XML comments can be parsed by a code editor (like Visual Studio) and displayed in a window.
C# program that uses XML comments using System; class Program { /// <summary> /// Test value and modify it. /// </summary> /// <param name="value">The important value.</param> /// <returns>The modified value.</returns> static int Test(int value) { return value * 2; } static void Main() { Console.WriteLine(Test(2)); } } Output 4
Performance, comments. When writing a program, comments do not affect the resulting program in any way. Excessive comments may make a program harder to read.

Here: The two methods are identical except that the first has no comments and the second has almost 20 lines of comments on it.

So: By testing these two methods, we can show what effect comments in methods has on execution performance.

C# program that tests comments class Program { static void Increment() { int value = 0; for (int i = 0; i < 100000; i++) { for (int j = 0; j < 10; j++) { value++; } } _field = value; } static void IncrementWithComments() { // Local variable in variable slot that is incremented. int value = 0; // Loop through the first one-hundred thousand numbers. for (int i = 0; i < 100000; i++) { // Nested loop through first ten numbers. for (int j = 0; j < 10; j++) { // Increment the variable once. value++; } } // Store local variable's value in static field. _field = value; } static int _field; static void Main() { Increment(); IncrementWithComments(); } } Output Increment: 0.331 ms (uncommented release) IncrementWithComments: 0.330 ms (commented release) Increment: 0.308 ms (uncommented debug, fewer iterations) IncrementWithComments: 0.308 ms (commented release, fewer iterations)
Benchmark. The methods were tested. To improve the quality of the results, the methods were called once before the timed loops to ensure they were JIT compiled.Benchmark

Result: The times measured were close for both the uncommented and commented versions of the methods.

Notes, compiler phases. Compilers are logically organized into phases, which are grouped into passes. A lexical parser converts the program text into a series of lexemes (words).Token

Tip: At this point, the compiler can remove all lines beginning with the // characters and text between the /* and */ delimiters.

And: Because of this phase, the compiled DLL from the C# code contains no trace of the comments.

Intent. It is usually recommended to describe at the level of intent, not the level of implementation. So you can comment on what the code is supposed to do.

Tip: The book Code Complete by Steve McConnell contains a large chapter on commenting strategies.

IL Disassembler. We can see how the C# compiler translates programs into the intermediate language. IL Disassembler reads in the compiled program and shows the abstract binary format.IL DisassemblerIL

Info: This code will have no comments or local variable names because these were removed.

A summary. Comments have no effect on the performance of C# programs. There are relevant compiler concepts and style issues. A variety of syntax forms can be used.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf