Comments in C#
Subject: C# Programming Course
Comments are ignored by the compiler and serve as notes to developers. They are use by programmers to make their code more readable. It can also be used to prevent execution of some lines of code when testing.
Single-Line Comments (//)
Used for short, inline explanations.
// Type of mathematical calculations I want to perform
Console.WriteLine("Addition");
//Add two numbers
Console.WriteLine(5 + 10);
Multi-Line Comments (/* ... */)
Used for longer explanations or disabling multiple lines of code.
/*
This program would display
student information
*/
Console.WriteLine("Name: Chioma Abiodun");
Console.WriteLine("Class: Primary 1");
Console.WriteLine("Gender: Female");
Difference between the WriteLine() and Write() method
Both WriteLine() and Write() are methods of the Console class in C#, used to display output on the console. However, they have a key difference in how they handle output formatting.
1.
Console.WriteLine(): prints or display the output and moves the cursor to the next line. Each call to WriteLine() starts output on a new line.
Console.WriteLine("Hello,"); // Moves to next line
Console.WriteLine("World!"); // Moves to next line
Output
Hello,
World!
2.
Console.Write(): prints the output without moving the cursor to a new line. The next output appears on the same line.
Console.Write("Loading");
Console.Write(".");
Console.Write(".");
Console.Write(".");
Console.WriteLine(); // Moves to next line
Console.WriteLine("Done!"); // Moves to next line
Output
Loading...
Done!
By:
Benjamin Onuorah
Login to comment or ask question on this topic
Previous Topic