Alright, time to start with the very beginning of the C#
syntax. Let's learn about some of the essential C# building
blocks. It all starts with statements.
The statements are the actions that make a program, and they are used
for creating actions such as declaring a variable,
calculating the sum of two values or displaying a string on the
console like we did in the previous module. That's the one you see
here on the slide. Statements are executed in a certain order. By
default, that would be vertically,
first statement, then the second statement, and so on.
The order is called the flow of execution.
That flow, however, won't always be the same though, as, for example,
input of the user might direct the flow to be different.
Different statements might be called next that is.
A statement always ends in a semicolon.
That's the first and important syntax rule. Here our statement is
just one line, but statements can actually be a series of
single‑line statements. In that case, they will be surrounded by
curly braces, and we'll see that in the next module. In C#,
whitespace and line breaks don't matter.
This means, in general, that you can add a break or whitespace without
making any difference in your code, and you're free to use these. So what
you see here will still work fine, although I do believe it is less
readable. I am sure you will agree. In C#,
we will give a name to, well,
quite a lot of things. Using that name, we can identify the item going forward.
We've seen in the previous module already the Console.ReadLine, which allows us
to capture a piece of text entered by the user.
Typically, we want to do something with that value, so we'll
need to give a name to that captured value.
That name is the identifier used to identify this string.
Many other items in C# will get an identifier. We'll see
soon that we of course need to name variables like the
input here, but also classes, namespaces, and more.
There's, again, some syntax learning here.
The identifier we can use can contain letters, digits, and underscores.
The first character of the identifier needs to be a letter
or an underscore, however, not a digit.
Therefore, this second statement, the red one, would be invalid, it wouldn't
compile, since the name of the identifier starts with a digit.
If we want to add in our code some extra information, we can use
comments. Using comments is definitely recommended.
They will be helpful for others using our code. In C#, there are different
ways to add this sort of extra information. In order to make the line to be
understood as comment, we need to add a double slash in front of it. From
then on, all text on that line won't be treated by the compiler as code.
It'll just be ignored, let's say.
Adding comments is definitely a good practice to make your
code more readable. If one line of code doesn't suffice for
the comment you need to add, which is perfectly fine,
you can of course add the double slash before every line. Now while that works,
it might be less reasonable, so a second option is available for commenting,
namely, multiline comments.
In this case, the comment is preceded with slash asterisk and followed
by asterisk slash. All content between these is treated as comments
and is therefore ignored by the compiler.
Just like most programming languages, C# comes with a number of keywords.
Those are reserved words with a special meaning, and that can't be changed.
C# 10 comes with more than 70 keywords. I have listed out a few here,
including if, void, class, and string. Keywords can typically be
used only in certain constructs, and, for example,
you cannot use them as an identifier. In C#, we'll often declare a
variable. Variables are storage locations, and they will hold a value.
Basically, it's a piece in the memory of your computer containing a
certain value. When we declare a variable,
we need to give it a name. That'll be the identifier for the
variable, again, and it must be unique for the context where it is
used. Using the name of the variable, we have access to the value it
holds. Variables are of a certain type,
which can be one of the many types in C#. So a variable can
be an integer, it can contain a string, a date, and so on.
Now this is the first time we encounter types in C#.
The type of a variable defines which value it can contain.
Remember that we said earlier that C# is a type‑safe language? Well,
this is related here. When we create a variable of a certain type,
C# will guarantee that the value is always of that specified type.
More on types very soon.
Creating a variable is done in a statement, more
specifically, a declaration statement.
Let's look at one,
shall we? And let us start with the declaration of an integer variable.
That's one of the most commonly used types. They contain integer numeric values.
Here we are declaring an integer variable of type int, which is the name used
to represent the integer type in C#. Next, we give the variable a name, and it
is age here. In general, variables will get a name that starts with a
lowercase. Lowercase and uppercase, however,
in C#, are different for the compiler, so age lowercase and Age
uppercase, as you can see here, are different variables.
Now while this will work,
I don't think this adds to the readability of the code and can actually be
pretty confusing. Giving variables a good and meaningful name isn't a
syntactical requirement, but it's definitely good practice.
Another convention in the naming of variables is the use of camel case. If
the identifier should actually be multiple words, use this notation. As you
can see here, the first word will always be lowercase and all subsequent
words start with an uppercase. That is camel case. Now we did say that
variables are storage locations,
named locations that can hold a value, but so far we have declared a
variable, but we don't have a value in there yet, so let's do that next.
Here we are assigning the value 25 to the age variable.
I'm using the equals sign for that, which is an operator, the assignment
operator. Using this, the value 25 will be stored in the variable.
Notice that this statement, too, needs to have a semicolon at the end.
Now since age is an integer variable, it can only be assigned integer values.
We can't put, for example, a string, so a piece of text, into this variable.
This, again,
has to do with that type safety. C# will only allow us to put
a value of the given type in the variable.
Now once we have declared our variable and assigned it a value,
we can also use it. Here we are creating a statement. I am going to write our variable to the console.