C# Hello World: Your First Program
If you are completely new to C#, a small console program is a useful place to begin. You can create it in Visual Studio or from the .NET CLI, run it, change the code, and observe what happens.
What you will learn
By the end of this guide, you will know how to create a console application, write a statement that produces output, run the application, and make a small change to the program.
1. Create a console project
In Visual Studio, create a new Console App using a current .NET SDK. You can also create one from a terminal with:
dotnet new console -n HelloWorld
cd HelloWorld
The command creates a project containing the files needed to build and run a simple console application.
2. Write the program
Console.WriteLine("Hello, World!");
Console.WriteLine writes a line of text to the console. The text between double quotes is a string literal. The semicolon marks the end of the statement.
3. Run it
In Visual Studio, start the project with the Run button. From the terminal, use:
dotnet run
You should see:
Hello, World!
If the command cannot find the project, make sure your terminal is inside the directory containing the .csproj file.
4. Make a change
Once the first program works, change it so that you can see how your edit affects the output.
Console.WriteLine("Welcome to C#");
Console.WriteLine(2 + 3);
The first statement prints text. The second evaluates an arithmetic expression and prints the result. Small experiments like this help connect the C# syntax to the output you see.
Common beginner mistakes
- Running
dotnet runfrom the wrong directory. - Accidentally removing a quote or semicolon from the statement.
- Editing one project while running a different project in Visual Studio.
What the program is doing
A console application starts executing its statements and sends the text from Console.WriteLine to the console. Although this example is small, the same basic idea appears throughout larger C# applications: your code calls APIs, works with values, and produces observable results.
Next step
Now learn how C# stores information with C# Variables and Data Types.
đŦ Comments
Sign in with Google to publish immediately, or comment anonymously and wait for approval.
Comments will appear here when available.