To understand how your code works, you need to step back and think about what a programming language is and how it communicates your commands to the computer.
What is a programming language?
Programming languages like C# let you write instructions that you want the computer to carry out. Each programming language has a different syntax, but after you learn your first programming language and attempt to learn a second one, you’ll quickly realize that they all share many similar ideas. A programming language’s job is to allow a human to write instructions in a human-readable and understandable way. The instructions you write in a programming language are called “source code”, or just “code”. At this point, a software developer can update and change the code, but the computer can’t understand the code. The code first must be compiled into a format that the computer can understand.
What is compilation?
A special program called a compiler converts your source code into a different format that the computer’s CPU can execute. When you used the green Run button in the previous unit, the code you wrote was first compiled, then executed. Why do we need to compile our code? Even though most programming languages seem cryptic at first, they can be more easily understood by humans than the computer’s preferred language, which is expressed by turning thousands or millions of tiny switches either on or off. Compilers bridge these two worlds by translating your human-readable instructions into a computer-understandable set of instructions.
What is syntax?
The syntax of a programming language includes the keywords, the operators (those special keyboard characters like the semicolon or parenthesis), and other grammar rules that the compiler enforces. The lines of code you typed followed about a dozen different syntax rules and used at least four different operators. There’s much to learn, but fortunately, each concept is simple.
Console.WriteLine("Hello World!");
When you ran your code, you saw that the message Hello World! was printed to the output pane. When the phrase is surrounded by double-quotation marks in your C# code, it’s called a literal string. In other words, we literally wanted the characters H, e, l, l, o, and so on, sent to the output.
The WriteLine()
part is called a method. You can always spot a method because it has a set of parenthesis after it. Each method has one job. The WriteLine()
method’s job is to write a line of data to the output window. The data that’s printed is sent in between the opening and closing parenthesis as an input parameter. Some methods need input parameters, others don’t. But if you want to invoke a method, you must always use the parenthesis after the method’s name. The parentheses are known as the method invocation operator.
The Console part is called a class. Classes “own” methods; or perhaps a better way to say it is that methods live inside of a class. To visit the method, you must know which class it’s in. For now, think of a class as a way to store and organize all of the methods that do similar things. In this case, all of the methods that operate on your Output pane are defined inside of the Console class.
There is also a dot, or period, that separates the class name Console and the method name WriteLine(). The period is the member access operator. In other words, the dot is how you “navigate” from the class to one of its methods.
Finally, the semicolon is the end of the statement operator. A statement is a complete instruction in C#. The semicolon tells the compiler that we’re finished entering the command.
Don’t worry if all of these ideas and terms don’t make sense. For now, all you really need to remember is that if you want to print a message to an output window like a console:
- Use
Console.WriteLine("Your message here");
- Capitalize
Console
,Write
, andLine
- Use the correct punctuation, because it has a special role in C#
References: https://docs.microsoft.com/en-gb/