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.
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 (like MyFirstApp) and click Next
- Choose the latest .NET version listed (like .NET 8.0 or .NET 9.0 or .NET 10.0) and click Create.
Step 2: Understand the Code
Visual Studio will automatically open a file called Program.cs. Inside, you will see this single line of code:
- 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.
Step 4: Change the Message
Click the green play button again, and you will see your new message on the screen!
With .Net CLI
Step 1: Create the Program
- Open your terminal or Command Prompt
-
Type the below command to create a new folder and project:
Static Code Layout Referencedotnet new console -n MyFirstProgram
-
Move into your new project folder
Static Code Layout Referencecd MyFirstProgram
-
Open this folder in VS Code by typing
Static Code Layout Referencecode .
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:
Static Code Layout ReferenceConsole.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
Static Code Layout Referencedotnet run -
Press Enter. The computer will compile (translate) your code and output
Static Code Layout ReferenceWelcome to CIIT Training Institute