Зміст курсу
C# Basics
C# Basics
C# Syntax
In this chapter we will look at the basic Hello World program to understand the syntax of the C# language.
Following is the code which prints out the message "Hello World" in the console output:
main
namespace TestConsoleApp { internal class Program { static void Main(string[] args) { System.Console.WriteLine("Hello, World!"); } } }
When you create a new C# project in Visual Studio or some other editor, you'll see this as the default boilerplate code with some minor differences.
Note
In programming, Boilerplate Code refers to standard sections of code that are often required for common tasks in programming, such as setting up configurations, initializing structures, or defining basic program structure. It adds necessary structure to a program but doesn't directly contribute to its core logic.
We don't need to understand all the code in detail at this level but we will look at some basic components of the code.
There are several code blocks in the above code. The curly brackets ({}
) mark the beginning and end of a code block. Each code block belongs to either a namespace, class, method or some statements.
A namespace is simply a container which holds related code together, helping us organize the program. In this program we have a namespace called TestConsoleApp
.
A class adds functionality to a program. It contains data and methods. In this program we have a class called "Program".
A method is a set of instructions that tells a computer to perform a specific task. It is similar to "functions" in other programming languages. It is not an issue in case you're unfamiliar with functions, everything will be explained in later sections. In this program we have a default "Main" method which is an important part of a C# program. The "Main" method is the starting point of the program. When we run the program it executes all the code inside the "Main" code block.
The "Main" method can have different forms for-example:
static public void Main(String[] args)
static public void Main()
protected static void Main()
private protected static void Main()
static void Main()
static void Main(String[] args)
We don't need to understand the differences between them at this level however it is important to note that the name of the method must be "Main". All the code we write in this section will be inside the Main codeblock.
A slightly simpler form of the main method which you can use throughout this course is: static void Main()
which excludes the string[] args
part. Hence the boilerplate code can be written as:
main
namespace TestConsoleApp { internal class Program { static void Main() { System.Console.WriteLine("Hello, World!"); } } }
Inside the "Main" method, there is a statement Console.WriteLine("Hello, World!")
. Here Console.WriteLine
is a command which tells the computer to output the text inside it. In this case we want to output "Hello, World!". When we run the program, this text is outputted in the console. We will learn more about this comment in the next chapter.
It is important to note that all the statements end with a semi-colon (;
).
To sum up, the basic starting boilerplate code of a C# program includes a "namespace" declaration, a "class" declaration, and the "Main" method, which serves as the program's entry point and contains the code to be executed. In some project templates it may also include a "using" statement to import necessary namespaces such as using System;
though it is not necessary.
Дякуємо за ваш відгук!