Understanding Syntax in C# Programming Language

C# programming

After writing our first program, it is time to know how its logic works.
C# Syntax
The syntax of C# code follows the following general rules:

  • C# statements must be terminated with semicolon ;
  • Code blocks are enclosed in curly braces {}. The Program class and the Main function are examples of code blocks. Their codes are enclosed in curly braces as you can see in the example. (If you don’t understand what a class or a function is, don’t worry, we’ll get to that later in this course).
  • As in C/C++ and Java languages, comments in C# can be added by preceding them with // (for single line comments), or by being enclosed between the /* */ sequences (for block comments). Comments are completely ignored by the C# compiler; they are a way to inform the reader of your code of something. For example:
// The following function compares two numbers
bool compare(num1,num2){
if (num1 > num2){
return true;
else {
return false;
/* This is a block comment. It can span to multiple lines and the compiler will completely ignore it at run time */

As a best practice, it is always recommended to document your code using comments. Comments increase the readability of your code. This will be greatly useful when you (or somebody else) need to modify your program in the future.
What is a namespace?
A namespace is simply a way to organize your code and prevent conflicts. Think of it as a person’s surname. For example if you are in a meeting and there are two persons named Ahmed, how can you distinguish the one you intend to communicate with? Yes, by referencing his surname, so you call Mr. Ahmed Hussein and it is the surname that differentiated him from the other one with the same first name.
Similarly in C#, you might have a function defined in your code named round. Later on, you noticed that this function conflicts with another function with the same name provided by C# Math class. When you call round(), which one of them will get called? It’s hard to say unless you place your round function in its own namespace, perhaps MyApplication for example. Now you can call MyApplication.round() and you are sure that the correct function will be called. Notice also the use of the dot (.) after the namespace name.
But what if you want to use a large number of functions (also called methods in C#) from MyApplication namespace, do you have to always precede them with MyApplication? Fortunately, no. You can use the keyword using MyApplication at the start of your file, and then start using your methods and objects without preceding them with the namespace.
Why all this using statements at the start of the file?
As mentioned, the using keyword gives you the ability to call methods without preceding them with a namespace. The System namespace is one of the most important namespaces in C#, most of the code you use is part of this namespace. For example, look at the part of your code that reads Console.WriteLine(“Hello World”). The Console class is part of the System namespace. So if you haven’t added using System at the start of the file, you would have to type System.Console WriteLine(“Hello World”). Similarly, other namespaces that Visual Studio included for us like System.Collections.Generic and the rest of the using statements. They are used to save you some typing time and effort nothing more.
Learn Cloud Computing from Scratch for Beginners
What is a class?
Classes and objects will be discussed in detail in a future article. You can regard them for now as another way to organize your code like namespaces. However, they are much more powerful than namespaces and provide a lot of functionality. They are the basis of what’s called “Object Oriented Programming”
The Main() function
A function is a block of code that does a specific task. It may return an output and it may not. If you want it to return an output, you must specify that in the function definition. For example:

int sum(num1,num2)
    return num1 + num2

This is function that returns a value of type integer. Hence, the int keyword is used before the function name. If you do not intend to return a value from your function, you precede the function name with the keyword void.
The Main() function in a C# program is the “entry point” of your application. It is where all other code in other files get called and executed.

  • In this article, we have explained the “Hello world” program (that we wrote in the previous article). In the light of this simple program, we have explained the basics of the C# syntax.
  • The program prints “Hello World” on the screen using the WriteLine() method. Console is part of the System namespace.
  • A namespace is a way to organize your code into units. A class is very similar to a namespace but it’s much more powerful.
  • A function is a block of code that does a task. The Main() function in C# is where the application starts.
  • A semi-colon is used to terminate C# statements.
  • It is wise to document your code using comments.
  • C# supports two types of comments: single-line comments using //, and multi-line comments using the /* */

I hope you enjoyed this article. In the next ones, we will discover more exciting features of C#. So stay tuned.


Please enter your comment!
Please enter your name here