Course Content
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 start a new C# project, you'll see some default code, known as boilerplate code. This code sets up the basic structure of your program but isn't part of the main logic.
Note
Boilerplate code is standard code needed for common tasks, like setting up configurations or defining the program's structure. It's important for organizing your code but doesn't affect the main functionality.
At this stage, you don't need to understand every detail, but let's look at some basic parts of the code.
The code is divided into blocks, marked by curly brackets ({}
). Each block is part of a namespace, class, method, or statement.
A namespace is like a folder that keeps related code together, making it easier to organize. In our example, the namespace is TestConsoleApp
.
A class is a blueprint for creating objects and adding functionality to a program. Here, we have a class named "Program".
A method is a set of instructions for the computer to perform a task. It's similar to "functions" in other languages. The "Main" method is crucial because it's where the program starts running. It executes all the code inside its block.
The "Main" method can look different, but it must be named "Main". For simplicity, you can use static void Main()
in this course. This version doesn't include string[] args
.
main
namespace TestConsoleApp { internal class Program { static void Main() { System.Console.WriteLine("Hello, World!"); } } }
Inside the "Main" method, there's a command Console.WriteLine("Hello, World!")
. This tells the computer to display "Hello, World!" on the screen. When you run the program, this message appears in the console.
Remember, every command ends with a semi-colon (;
).
In summary, the basic starting code for a C# program includes a "namespace", a "class", and the "Main" method, which is where the program begins. Sometimes, it might also have a "using" statement like using System;
to include necessary libraries, but it's not always needed.
Thanks for your feedback!