Unlike python, in C# semicolon is compulsory after every statement. The compiler would indicate an error if we do not use a semicolon. In C# language, we have to import the relevant libraries to use the built-in functions. But if we are coding in Visual Studio and created a C# framework then there is no need to import any library because the libraries will already exist in our code when we create a package. And they will be blurred but when we use any method of a specific class the IDE will automatically change the color of the text and make it visible in the code. Whenever we use any other IDE for C# coding, we have to write it manually. IDE is the abbreviation of an integrated development environment and it permits us to code in a particular language. For C# coding the most used and trusted IDE is Visual Studio code.
The syntax of C# language is very well-ordered and maintained, each is readable and clear. The code will not import any extra files or include any useless variables because it does not allow us to define pointless variables that are existing but never used in the program. And if we declare a variable but do not use it throughout the entire code, it will continue to state that the variable is declared but not utilized.
Syntax from Example 1:
In this example, we will utilize only one namespace in the code. And let’s cover every single detail you need to know about C# syntax and execution from scratch.
Code:
namespace program_0
{
class Prog
{
static void Main(string[] args)
{
Console.WriteLine("This is the first code!");
}
}
}
Output:
Explanation of Code
Let us go through each line of the above code in depth, how it works, and why we need it in our code.
Using System:
The statement ‘using System’ tells the compiler that we are using the System: it is a namespace that we will discuss in detail but here you just need to understand that before using anything in code we have to tell the compiler that we are using the system in our program. When we create a package or project, we must first write this statement. This statement allows us use everything the system has. We can easily access all the classes and built-in methods of the system, without writing the word ‘system’ again and again. Like if we want to show something on the console then we will employ the Console.Write() function. We call this function in such a way Console.Write() when we write ‘using System’ at the start of the code. But when the statement ‘using System’ is not mentioned at the start, we will employ a function like System.Console.Write(). We must include the word ‘system’ with any system function that we employ. But by utilizing the statement ‘using System’ at the start of the code, we can save time.
Blank line
The coding in C# language is well managed from classes to indentation. The compiler takes care of everything. Whitespaces are ignored by C#, thus adding extra blank lines makes the code easier to understand without affecting how the code functions. They play an important role and enhance the readability of the code. After the statement ‘using System’, the blank line is used to separate the portion of importing libraries from the other code.
Namespace
The term ‘namespace’ works as a container that contains the main code, classes, and functions. Within one project we can have more than one ‘namespace’. And outside this ‘namespace’ we can also define other namespaces. But for doing this we have to define those by using ‘namespace_name’, which shows the name of the new namespace that is created. We always use ‘namespace’ with the name of the class that we want to access in the code. By default, the name of the project that we created is assigned the ‘namespace’ name. We can change it but for that, we need to update the folder’s name. It organizes data members of one namespace just like a class but in ‘namespace’ we can have more than one class. When a ‘namespace’ is created in a project it contains one class name ‘Program’, we can modify this name which is called a namespace class member. And inside the class, we have a method that is called a member method of the namespace.
Curly Braces
Within the curly braces of a namespace, we write the main code. The code inside the namespace includes the classes, functions, and instances of classes. A namespace can have another namespace in it, which is called a nested namespace. The methods or members of a namespace can be accessed by employing the ‘namespace’ with a dot. By doing this, we can call any function: namespace_1.MyProgram(). The compiler will then access the namespace that we have mentioned in the code, the dot will tell the compiler that we want to access its method or member and after the dot specifies the name of a method that we want to access.
Class
A class is an organizer inside the namespace that will organize data members and member methods. A class contains different data types and methods in one place that can be accessed by the objects of the class. The C# language provides a built-in class where we can employ several methods. But one main method will be utilized to call all the methods or classes. In C#, it is possible to have a class inside of another class; this is known as inheritance. This concept of classes is called Object Oriented Programming (OOP) in which we create objects of a class. In OOP we have different methods like encapsulation for hiding data, an inheritance that increases the security, and inheriting the data in form of a parent-children relationship, one parent can have many children but one child has only one parent, abstraction, and a lot other. In C#, classes are more important because if we want to add two numbers, we do it with the help of classes as it has a class when we create a project. Everything we do is inside a class.
Main() Method
One program has only one Main() method. In this Main() method, we call all the objects and functions, we can assign values, and perform operations. We can create many classes and namespaces but the namespace that is created with the project will have a class. Within that class, we invoke the Main() method. Everything that is presented on the output screen is due to this method, the objects we called in the Main() method will show additional data but methods will not be visible on the output screen. In C#, we can call more than one Main() method, but to do so, we must tell the compiler which Main() method will be executed and compiled. For that, we have to compile as StartupObject compile.
Console
The console is the output screen; it is the class of the namespace of the System. It is used to show messages and the value of variables on the output screen. The console has many methods because it is a class so it has built-in methods that we can use by just calling the method with the class name, Console.WriteLine(). Here, ‘console’ shows the name of a class that is using the System namespace and WriteLine() is the method of that class which we are using. An important thing to note here is that if we do not utilize the ‘using System’ statement at the beginning of the code, we must always include the term ‘system’ with the function name when calling a method from the system namespace. This tells the compiler that it is a class of system and we are using a specific method of that class. Numerous methods are built into the System so that we can easily access them. It includes Console.ReadLine(), Console.Write(), Console.BackgroundColor() and a lot more that we can employ to make changes in our console. All the methods in the ‘Console’ class are integrated to make variations in the console.
Syntax from Example 2:
Here, we employ more than one namespace in our code to demonstrate how it functions when there are multiple namespaces.
Code:
namespace one_1
{
public class MyOne
{
public static void OneMethod()
{ Console.WriteLine("This is MyOne class of namespace one_1 ");}
}
}
namespace program_1
{
class Program
{
static void Main(string[] args)
{
one_1.MyOne.OneMethod();
Console.WriteLine("This is namespace program_1");
}
}
}
Explanation of Code:
We will now learn how we can create namespaces in the C# of our choices. There is already one namespace created in the C# IDE when we create a project, it automatically sets an environment for us to code in C# language. We can create multiple namespaces if we feel that we need another namespace to separate one part of the code from the other.
In this program, we will create another namespace to show you how it works. The program created will be saved as ‘program_1’ and the C# will set an environment with a namespace, class, and a Main() method. Then, we created a namespace of ‘one_1’. Inside that namespace, we created a public class because we cannot do coding in the namespace without defining the class. So, we need to define a class name ‘MyOne’. Within this class, we can invoke multiple methods but here we call only one function to decrease the complexity of the code and make it simple and easy to understand. The OneMethod() function is called inside the ‘MyOne’ class. Then, we employed a method of System class of namespace ‘Console’ to represent a message on the terminal. The Console.WriteLine() function shows the text ‘This is MyOne class of namespace one_1’ whereas WriteLine() sends the cursor to the next line after printing the message. Now, go into the namespace ‘program_1’ created by the system environment. Inside the class ‘Program’, the Main() method call the namespace method OneMethod(). When we invoke a function of a namespace in the Main() method, we write it in such a way. Namespace_name.class_name.method_name(); In the code, we implement this function as one_1.MyOne.OneMethod(). Here the attribute ‘one_1’ is the namespace name, ‘MyOne’ shows the class of that namespace and OneMethod() is the name of a method. After this, the Console.WriteLine() function is applied to print the statement ‘This is namespace program_1’ on the terminal.
Let us see how this code works. The compiler will start compiling from the Main() method. The first statement in the Main() method will be executed first, it will go to the namespace ‘one_1’. Then, it will go to the class ‘MyOne’ of this namespace and will execute the function OneMethod() of this class. The OneMethod() has only one statement in it that will be implemented and then the control will go back into the Main() method and executes the next statement. Thus, we will get a message on the terminal.
Output:
The output screen will show two messages on the console, one from the default namespace and the other from the namespace that will be created. The first declaration in the Main() function would be executed first and then the next statement will be executed. These two statements will print two messages by calling the Console.WriteLine() function.
Conclusion
We have discussed the basic syntax of the C# in detail. This article contains everything you should know before coding in C# language. This language is case-sensitive, and the semicolon is important to terminate every statement. You may get an error if you forget to put the semicolon. In the C# namespace, the classes are very important, they keep the code organized. We can define different namespaces to separate one part of the code from the other. With a number of examples, the basic syntax of C# has been thoroughly explored. In a nutshell, the syntax of C# is a little different from other languages but it is more reliable and readable. But once you understand its syntax, the coding in C# becomes much easier for you.