C# Hello World: Create and Run Your First Console Program
- Get link
- X
- Other Apps
Write and run your first C# console program. This lesson includes the commands, complete code, expected output, and fixes for common beginner errors.
Before you start
Install the .NET SDK from Microsoft's official website. An editor alone cannot compile C#. Open a terminal and run dotnet --version. A version number confirms that the command is available.
Create a console project
dotnet new console -n HelloCourse
cd HelloCourse
dotnet runThe template creates a project file and Program.cs. Run the commands one line at a time. The final command builds and runs the project in the current directory.
A complete program with Main
Replace the contents of Program.cs with this code. Use this version if your course explains classes and the Main entry point.
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}Save the file and run dotnet run again. Expected output:
Hello, World!using System lets us use Console without writing its full namespace. Program contains the Main method. Main is the explicit entry point here; static means it can run without creating a Program object. WriteLine prints text and then a line break. The semicolon ends that statement.
Why your template may look different
Modern C# console templates can use top-level statements. Their Program.cs may contain only Console.WriteLine("Hello, World!");. That is valid too: the compiler provides the entry point. Choose either the short template or the explicit Main version for this exercise. Do not paste both versions together.
Make it your own
Change the message to the name of a course. Add a second WriteLine statement for a learning goal. Predict which line appears first, then run the program to check. Next, compare Write and WriteLine: Write keeps subsequent output on the same line.
Troubleshooting
- dotnet is not recognized: confirm the SDK is installed and open a new terminal after installation.
- No project found: use
cd HelloCourseand check that the folder contains a .csproj file. - Compiler errors: check matching braces, straight quotation marks, exact capitalization of Console.WriteLine, and the final semicolon.
- Old output: save Program.cs before running again.
Original video lesson
Next lesson and reference
Continue with calling C# methods and displaying messages. For the official setup walkthrough, see Microsoft's console application tutorial.
- Get link
- X
- Other Apps
Comments
Post a Comment