初心者のためのC


At the end of this lesson, you should be able to answer the following:

  • What are comments? Why should I use them?
  • How do I write comments in C#?

プログラムはコンピュータのために書かれますが、時々、他のプログラマが我々のコードを読んで、理解するのを助けるために、より多くの文脈を加える必要があります.(プログラマが自分自身であることもあります).
これを行うには、我々のプログラムでコメントを書くことができます.コメントは、プログラムが実行されているときにCが無視されるテキストの行です.
良いコードは、通常、自己説明ですが、コメントは、プログラムが特定の方法で書かれている理由を説明するのに便利です.
コメントを書くには// , コメントを続けます.アフターオール// は次の行まで無視される.
// My first program
Console.WriteLine("Hello, World!");
コードボックスの中に入力した後、上記のコードを実行します.My first program 表示された出力には表示されません.

また、インラインでコメントを書くこともできます// .
// My first program
Console.WriteLine("Hello, World!"); // this prints out a message
コメントは、実行したくないコードのビットを取り出すのに便利です.たとえば、コード内のエラーの原因(デバッグと呼ばれる)の原因を見つけようとすると、エラーを発生しないことを確認したいコードをコメントアウトできます.
Console.WriteLine("Yesterday it worked.");
Console.WriteLine("Today it is not working.");
// Console.WriteLine("This line will not print.");
Console.WriteLine("Coding is like that.");
コメントを書くもう一つの方法は、複数行コメントです.名前が示すように、複数行にまたがるコメントを書くことができます.
マルチラインコメントは/* で終わる*/ . これらの2つの要素の間のすべては無視されます.
Console.WriteLine("My favourite dinosaurs:");
Console.WriteLine("Triceratops");
Console.WriteLine("Velociraptor");
/*
Console.WriteLine("Mosasaurus");
Console.WriteLine("Dimetrodon");
Console.WriteLine("Pteranodon");
*/
Console.WriteLine("Apatosaurus");
Console.WriteLine("Tyrannosaurus Rex");

Challenge

Add some comments to your answers to the Lesson 1 challenges!