C# Syntax

C# has many nitty gritty details and throughout this tutorial we will explain them one by one. But before that, we need to get a general sense on how a C# program looks like.

So in this section we will give a general view about the syntax of a C# program.

Take a look at the example below:

using System;

namespace quickExample

{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

If we compile and run this example, we will see the message “Hello World” on the screen.

The first line of this example is using System;:

First of all this is called a statement. Statements have different forms but they all end with semicolon ;. If you ignore the semicolon, you’ll get an error and your program won’t compile and run correctly.

Alright, now let’s see what this statement does:

In programming languages like C# we mostly work with prebuilt packages and functionalities. For example if we want to send a message to the console, we can use the Console.WriteLine (explained in a moment).

By default these packages and functionalities are not part of the program that we’re working on. Basically we need to manually import any of them that might be needed the program.

This is where the using keyword comes in.

In the using System; statement, we’re basically telling the C# engine that we need and want to USE the System package (AKA namespace) in our program.

Inside this namespace there are many prebuilt class and functions that can be used in a program. For example, to send a message to the console we use the Console.WriteLine. Now guess where this Console class is coming from? Well it is the System namespace.

So by invoking the System namespace, we’re making this namespace as part of our program and will be able to use its prebuilt members whenever they’re needed.

The next line in our program is:

namespace quickExample{…}

Starting with the keyword namespace:

This keyword is used to create a new namespace in a program.

A program can grow fast and become multiplex. For example part of a program might be in charge of controlling the communication with the data base, another part might be in charge of user interface etc. and we can’t put all these parts in one place! Well, technically we can but then the maintenance can easily become a nightmare! For this reason we use namespaces to separate parts of a program from each other and essentially organize them better.

So here with the help of namespace keyword we’ve created a new namespace with the name quickExample.

Basically first comes the namespace keyword and followed by that is a white space and after that is the name of the namespace that we want to add.

The pair of braces {} that comes after the name of the namespace declare the body of that namespace. Anything that we declare in this body belongs to the target namespace.

Now if in the future, this namespace was needed in another program, we can use the using keyword and the name of the namespace to invoke it in the target program.

Note: in the namespace and the using keyword sections we will explain the concept of namespace in greater details.

The next line in our program is:

class Program{…}

C# is object oriented. This means anything in a C# program is treated as object (There are exceptions to this as well which you’ll see in later sections). But before an object can be created in a program it first needs to have a blueprint or a map (just like real world objects like cars, houses etc. that need to have blueprints before they’re built).

In C# the blueprint of an object is called class! Whenever we want to create an object, first we build (or use a prebuilt) class and create objects from that class.

So here in the class Program{…} we’ve basically created a class with the name Program.

The keyword class always comes before the name of that class. Also we use a pair of braces {} to define the body of the target class. So anything between the open brace { and the closing brace } of the Program class belongs to this class.

The next line in this program is:

static void Main(string[] args){…}

This is called method or sometimes people call it function as well.

What is a method and what is does?

Well a method is the place where we define the functionalities of a program. There can be one or many methods in a program and each in charge of doing part of a total work of that program.

For example we might have a calculator program. In this program there might be multiple methods:

  • One in charge of addition process.

  • One in charge of subtraction process.

  • Another one for multiplication and so on…

Again, in short, a method is the place where we define a set of instructions to be executed whenever that method is called.

Methods have name. The name of this particular method is Main. There are two keywords behind the name of this method and these are static and void.

void: This keyword declares that the method does not return a value.

static: this keyword declares that the method can be invoked directly by the name of the class without creating an object.

Notes:

  • In any C# program, there should be one method with the name
    `Main` and the same signature that you see here. C# engine always starts
    from the `Main` method and runs instructions from its body. Basically
    this is the entry point of our program.

  • Any method has a pair of parentheses `()` after its name. This is
    part of the signature of methods. (Just so you know, in the parentheses
    we can put parameters as well. But this is something that you’ll learn
    about in the method section)

  • A method also has its own body and this area is between the open
    and close braces`{}` that comes after the parentheses of the
    method.

Inside the body of the Main method we have this statement:

Console.WriteLine("Hello World!");

Console is the name of a class and the WriteLine is a static method in that class. Basically this statement is telling the C# engine to invoke the WriteLine method of the Console class.

Note that we used the dot . operator between the name of the class and its method.

The WriteLine() method is mainly used to send a value to the console. This method is basically representing the programs output.

In the parentheses that come after this method, we put the value that we want to send to the output.

For example if we wanted to send the value 10000 to the output, all we need to do is to put this value in the parentheses of this method:

Console.WriteLine(10000);

Note: in C# data has different form. For example String values like "Hi" is different than an integer value like 12345. You’ll learn these types in the data type section.

In the Main method there’s only one statement. So when the C# engine runs our program, it only executes this single statement and after that the program will terminate because there’s no other statement in the Main method to be executed.

Again, the Main method is the entry point to a C# program and as long as there’re instructions in this method, the program will continue to run. But when C# engine reaches to the end of this method (closing brace }), it is equal to the end of the program and it will be terminated after that.

At the end of this section, we should mention that C# is case sensitive. This means the name Main is different than the name main or MAIN. Or the keyword static is different than the Static or STATIC keywords etc.

All the stuff you read in this section will be explained in greater details one by one in later sections. The purpose here was to give you a general view of what a simple C# program looks like. So don’t worry if many concepts in this section didn’t click into place right away.

You’ll learn as you go along.