C# Program Structure

Writing your first C# Program.

* Before start compiling the raw C# code, you need to create a work environment or a typical project structure after that you can compile your C# code.

C# Compile Codes

To create and compile your first C# console program using the .NET SDK, you need to follow these steps. I'll guide you through creating a simple "Hello, World!" program.

Create C# Project:

First, you need to choose a specific patch to create your application after opening a command prompt or terminal and running the following commands:

bash Copy Code
dotnet new console -n HelloWorld
cd HelloWorld

This will create a new folder named 'HelloWorld' with a basic C# console application.

Edit the C# Code:

Open the generated 'Program.cs' file in your preferred code editor. You can use Visual Studio Code, Visual Studio, or any other code editor you like. Replace the content of 'Program.cs' with the following:

cs Copy Code
//Write "Hello, World!" program in C#
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}

Save the changes you made to 'Program.cs'.

Compile and Run:

In the command prompt or terminal, run the following command:

bash Copy Code
dotnet run

You should see the output:

Output:
Hello, world!

Congratulations! You've successfully created and compiled your first C# program using the .NET SDK.

Let's break down each line of the "Hello, World!" program:

// The '//' symbol is used to denote a single-line comment. Anything following the '//' on the same line is treated as a comment and is ignored by the compiler.

Note: Multiline comments are enclosed between '/*' and '*/'. Everything between these delimiters is treated as a comment and is ignored by the compiler.

using System; This line is an 'using' directive, which allows the program to use types and members from the 'System' namespace without fully qualifying them.

class Program This line defines a class named 'Program'. In C#, the 'Main' method, which is the entry point of a console application, is typically located within a class called 'Program'.

{} The curly braces '{}' are used to define blocks of code and to group statements. These curly braces define the scope of variables, methods, classes, and other constructs in the C# language.

static void Main() This line declares a 'static' method named 'Main'. The Main method is the entry point of the program and is executed when the program starts. It has a return type of 'void', indicating that it does not return any value.

'static' is a keyword in C# used to declare a member (such as a method or field) that belongs to the type itself rather than to a specific instance of that type. In the context of the 'Main' method, it means that 'Main' is associated with the type (class) rather than with an instance of the class.

'void' is a keyword used as the return type of a method to indicate that the method does not return any value. In the case of the 'Main' method, 'void' means that 'Main' does not produce any result value.

'Main' is the name of the method. In C#, the 'Main' method is the entry point for a console application. When the program starts, it looks for the 'Main' method and begins executing the code inside it.

Console.WriteLine("Hello, World!"); This line calls the 'WriteLine' method of the 'Console' class to display the text "Hello, World!" on the console. The 'Console' class provides methods for interacting with the console, and 'WriteLine' is used to output a line of text followed by a newline character.

'Console' is a class in the 'System' namespace in C#. It provides basic input and output services, particularly for console-based applications. The 'Console' class includes methods for reading from and writing to the console.

'WriteLine' is a method of the 'Console' class. It is used to write a line of text to the console. The WriteLine method appends a newline character ('\n') after the specified text, moving the cursor to the beginning of the next line.

; the semicolon (';') is used as a statement terminator. It indicates the end of a statement. In C#, each statement must be terminated with a semicolon. Statements are the individual instructions that make up a program.

* In summary, this C# program defines a class called 'Program' with a 'Main' method that prints the message "Hello, World!" to the console when the program is executed.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.