Introduction
In this chapter, we will learn how to create and run your first C# program: Hello World. This simple program will introduce you to the basic structure of a C# application and how to compile and run it using different development environments.
Creating Your First C# Project
Using the .NET CLI
The .NET Command Line Interface (CLI) is used for creating, building, and running .NET applications. Follow these steps to create and run your first C# program using the .NET CLI:
-
Open a Command Prompt or Terminal.
-
Create a New Project: Run the following command to create a new console application:
dotnet new console -n MyFirstApp
This command creates a new directory called
MyFirstApp
with a basic C# console application. -
Navigate to the Project Directory:
cd MyFirstApp
-
Run the Project:
dotnet run
This command compiles and runs the application, displaying "Hello World!" in the console.
Using Visual Studio
Visual Studio is a powerful Integrated Development Environment (IDE) for C# development. Follow these steps to create and run your first C# program using Visual Studio:
-
Open Visual Studio.
-
Create a New Project:
- Click on "Create a new project".
- Select "Console App (.NET Core)" and click "Next".
- Name your project "MyFirstApp" and choose a location to save it.
- Click "Create".
-
Write Your Code:
- Visual Studio automatically generates a basic console application. Open the
Program.cs
file, which contains the following code:
using System; namespace MyFirstApp { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }
- Visual Studio automatically generates a basic console application. Open the
-
Run the Project:
- Click the green "Start" button or press
F5
to build and run the application. The output window will display "Hello, World!".
- Click the green "Start" button or press
Using Visual Studio Code
Visual Studio Code is a lightweight, cross-platform code editor that supports C# development through extensions. Follow these steps to create and run your first C# program using Visual Studio Code:
-
Open Visual Studio Code.
-
Open the Terminal: Go to
View > Terminal
or pressCtrl+`
to open the integrated terminal. -
Create a New Project:
-
In the terminal, run:
dotnet new console -n MyFirstApp
-
-
Open the Project:
- Open the project folder in Visual Studio Code by going to
File > Open Folder
and selecting theMyFirstApp
directory.
- Open the project folder in Visual Studio Code by going to
-
Write Your Code:
- Open the
Program.cs
file, which contains the following code:
using System; namespace MyFirstApp { class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } }
- Open the
-
Run the Project:
-
In the terminal, run:
dotnet run
This command compiles and runs the application, displaying "Hello, World!" in the terminal.
-
Understanding the Code
Here’s a breakdown of the basic C# program:
using System;
namespace MyFirstApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
using System;
: This statement includes theSystem
namespace, which contains fundamental classes for input and output operations.namespace MyFirstApp
: A namespace is used to organize code and prevent naming conflicts.class Program
: A class is a blueprint for creating objects. TheProgram
class contains the code for our application.static void Main(string[] args)
: TheMain
method is the entry point of the application. It is where the program starts executing.Console.WriteLine("Hello, World!");
: This line prints the string "Hello, World!" to the console.
Conclusion
Congratulations! You have successfully written and run your first C# program. This simple "Hello, World!" application introduces you to the basic structure of a C# program and how to use different development environments to create and run C# applications. In the next chapters, we will dive deeper into the fundamentals of C# programming, including variables, data types, control flow, and more.