Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
C# Syntax | Getting Started
C# Basics
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

book
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:

cs

main

copy
12345678910
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.

cs

main

copy
12345678910
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.

1. What is the purpose of the "Main" method in a C# program?
2. What does the command `Console.WriteLine("Hello, World!")` do?
What is the purpose of the "Main" method in a C# program?

What is the purpose of the "Main" method in a C# program?

Select the correct answer

What does the command `Console.WriteLine("Hello, World!")` do?

What does the command Console.WriteLine("Hello, World!") do?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt