Your First C# Program
To create your very first program in .NET, you will make a Console Application. This is a simple program that runs in a text window (terminal) and prints a message on the screen.
Here is how to create, understand, and run your first program using the VS Code and .NET CLI.
Creating and running your first program in Visual Studio is easy because the software handles the background work for you. Here is the step-by-step guide to doing it in the full Visual Studio IDE on Windows.
Create Your First Program Using Visual Studio
Step 1: Create a New Project
- Open Visual Studio.
- On the startup screen, click Create a new project on the right side.
- In the search bar at the top, type Console.
- Select Console App (make sure it has the C# logo next to it), then click Next.
- Give your project a name, such as MyFirstApp, and click Next.
- Choose the latest .NET version listed (like .NET 8.0, .NET 9.0, or .NET 10.0) and click Create.
MyFirstApp or HelloWorld
are easy to understand.
Step 2: Understand the Code
Visual Studio will automatically open a file called Program.cs. Inside, you will see this single line of code:
using System;
Console.WriteLine("Hello, World 🌍..!");
This simple statement displays text on the console.
- Console: This means the text window on your screen.
- .WriteLine: This tells the computer to "write a line of text."
- ("Hello, World!"): This is the exact message the computer will show.
- ; (Semicolon): This acts like a period at the end of a sentence. It tells the computer the instruction is over.
Step 3: Run the Program
- Look at the top toolbar in Visual Studio. Find the green play button that says MyFirstApp (or has the name of your project).
- Click that green play button or press the F5 key on your keyboard.
- A black text window (the console) will pop up on your screen, display Hello, World 🌍..!, and stay open so you can see it.
- Press any key on your keyboard to close the window.
Expected Output
Hello, World 🌍..!
Step 4: Change the Message
You can change the text inside the quotation marks to display your own message.
using System;
Console.WriteLine("Welcome to CIIT Training Institute 👨🏫❤️...!");
Click the green play button again, and you will see your new message on the screen!
Output
Welcome to CIIT Training Institute 👨🏫❤️...!
Understanding the Program
The following example shows a basic C# console program with a class and a Main method.
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World 🌍...!");
}
}
| Code | Purpose |
|---|---|
using System;
|
Allows the program to use types from the System namespace. |
class Program
|
Defines a class named Program. |
static void Main()
|
Defines the main entry point of the traditional C# console program. |
Console.WriteLine()
|
Displays a message on the console. |
Create Your First Program with .NET CLI
The .NET CLI allows you to create and run C# applications directly from the terminal without opening Visual Studio.
Step 1: Create the Program
- Open your terminal or Command Prompt.
-
Type the below command to create a new folder
and project:
Terminal
dotnet new console -n MyFirstProgram -
Move into your new project folder:
Terminal
cd MyFirstProgram -
Open this folder in VS Code by typing:
Terminal
code .
Step 2: Look at the Code
Inside VS Code, you will see a file named Program.cs. Click to open it. By default, .NET creates a simple one-line program for you that looks like this:
using System;
Console.WriteLine("Welcome to CIIT Training Institute 👨🏫❤️..!");
Step 3: Run the Program
- Go back to your terminal or open the built-in terminal inside VS Code by pressing Ctrl + `.
-
Type the run command:
Terminal
dotnet run -
Press Enter. The computer will compile
(translate) your code and output:
Output
Welcome to CIIT Training Institute 👨🏫❤️..!
Understanding the .NET CLI Workflow
The basic workflow for creating and running a console application using the .NET CLI is:
dotnet new console -n MyFirstProgram
↓
cd MyFirstProgram
↓
code .
↓
Write / Edit C# Code
↓
dotnet run
↓
View Output
This workflow is useful because the same CLI approach can be used for many types of .NET projects.
Visual Studio vs .NET CLI
| Visual Studio | .NET CLI |
|---|---|
| Graphical development environment | Command-line based development |
| Project creation through menus | Project creation through commands |
| Built-in debugging and project tools | Lightweight and fast |
| Suitable for complete IDE-based development | Useful for terminal, automation and cross-platform workflows |
Common Beginner Mistakes
| Problem | What to Check |
|---|---|
| Missing semicolon |
Make sure statements such as
Console.WriteLine()
end with ;.
|
| Wrong project folder |
Use cd to move into the
correct project directory before
running dotnet run.
|
| .NET command not working |
Check whether the .NET SDK is installed
using dotnet --version.
|
| Code changes not visible | Save the file and run the application again. |
Summary
In this topic, you created your first C# Console
Application using Visual Studio and the .NET CLI.
You learned how to create a project, understand
Program.cs, use
Console.WriteLine(), change the output
message, run the application, and view the result
in the console.
You also learned the basic .NET CLI workflow using
commands such as
dotnet new,
cd,
code .,
and dotnet run.
These fundamentals provide the starting point for
learning variables, data types, operators, control
flow, methods, and other C# programming concepts.