How did your code work?
Let’s focus on the following line of code you wrote:
C#Copy
Console.WriteLine("Hello World!");
When you ran your code, you saw that the message Hello World! was printed to the output console. When the phrase is surrounded by double-quotation marks in your C# code, it’s called a literal string. In other words, you literally wanted the characters H, e, l, l, o, and so on, sent to the output.
The Console part is called a class. Classes “own” methods; or you could say 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 represent an object. In this case, all of the methods that operate on your output console are defined inside of the Console class.
There’s 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.
The WriteLine() part is called a method. You can always spot a method because it has a set of parentheses after it. Each method has one job. The WriteLine() method’s job is to write a line of data to the output console. The data that’s printed is sent in between the opening and closing parenthesis as an input parameter. Some methods need input parameters, while others don’t. But if you want to invoke a method, you must always use the parentheses after the method’s name. The parentheses are known as the method invocation operator.
Finally, the semicolon is the end of statement operator. A statement is a complete instruction in C#. The semicolon tells the compiler that you’ve finished entering the command.
Don’t worry if all of these ideas and terms don’t make sense. For now, all you need to remember is that if you want to print a message to the output console:
- Use
Console.WriteLine("Your message here"); - Capitalize
Console,Write, andLine - Use the correct punctuation because it has a special role in C#
- If you make a mistake, just spot it, fix it and re-run
Tip
Create a cheat sheet for yourself until you’ve memorized certain key commands.
Understand the flow of execution
It’s important to understand the flow of execution. In other words, your code instructions were executed in order, one line at a time, until there were no more instructions to execute. Some instructions will require the CPU to wait before it can continue. Other instructions can be used to change the flow of execution.
Now, let’s test what you’ve learned. Each module features a simple challenge, and if you get stuck, you’ll be supplied with a solution. In the next unit, you’ll get a chance to write some C# on your own.