The rules for writing C# code is called syntax. Just like human languages have rules regarding punctuation and sentence structure, computer programming languages also have rules. Those rules define the keywords and operators of C# and how they are put together to form programs.
When you wrote code into the .NET Editor, you may have noticed subtle changes to the color of different words and symbols. Syntax highlighting is a helpful feature that you’ll begin to use to easily spot mistakes in your code that don’t conform to the syntax rules of C#.
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.
database training courses malaysia