Codectionary / Developer documentation / C#

Your first C# program

Create a .NET console project, write and run a C# greeting, understand the syntax, and resolve common first-project errors.

Before you start

Install a supported .NET SDK, not just the runtime. This walkthrough was checked against the .NET 10 SDK and its console template. In a terminal, run dotnet --info: it reports the SDK version, operating system, architecture and installed runtimes. Look for a 10.x SDK. If you use another SDK, inspect the generated project file for its TargetFramework rather than assuming that every template targets .NET 10.

You can use PowerShell on Windows or Terminal on macOS or Linux. A terminal accepts commands; an editor lets you change source files. You do not need Visual Studio for this first app.

Check the installed SDK

dotnet --info

Create a folder and console project

Open your terminal in a location where you can create a practice folder. mkdir creates FirstCSharp; cd enters it. dotnet new console creates a C# console project in the current folder and normally restores its dependencies. With the .NET 10 SDK selected, the generated project targets net10.0. Keep the terminal in this folder for dotnet run.

You can open FirstCSharp in an editor using its Open Folder command. Open the whole folder so the editor can see both Program.cs and the project file. If your shell says dotnet is unknown, reopen the terminal after installing the SDK and return to the operating-system installation guide.

  1. Open a terminal in a writable location.
  2. Create and enter the FirstCSharp folder.
  3. Run dotnet new console, then open that folder in an editor.

PowerShell or a Unix shell

mkdir FirstCSharp
cd FirstCSharp
dotnet new console

Understand the generated files

Program.cs contains the C# source that the template runs. In the .NET 10 console template, it starts with a comment and Console.WriteLine("Hello, World!");. The FirstCSharp.csproj project file tells the SDK that this is an executable project and which .NET target framework to build for. The generated file names follow your folder name unless you choose a different project name.

The SDK may create an obj folder during restore and a bin folder when it builds. Those hold generated intermediate and output files; edit Program.cs rather than editing build output. A solution file is optional for this one-project command-line example.

Write Hello World and run it

Replace the contents of Program.cs with the single line below and save the file. Console is a .NET class for standard input and output. The dot selects its WriteLine method, which writes the supplied text and a newline to the terminal. Parentheses contain the argument passed to the method. Double quotation marks make the message a string; they are not printed. The semicolon ends this C# statement.

Run dotnet run while your terminal is in FirstCSharp. This command builds the project when needed, then launches it. The expected output is Hello, Codectionary! on its own line. Build messages may also appear, but they are not output from Console.WriteLine. This program writes to a terminal, not a browser page. Codectionary’s Playground can display C# source but does not currently execute it.

Program.cs — replace the template content

Console.WriteLine("Hello, Codectionary!");

Terminal — inside FirstCSharp

dotnet run

Change the program and predict the result

Add a second WriteLine statement, save Program.cs, and run dotnet run again. Each call writes one line, so the second message appears below the greeting. This tiny change checks that you are editing the file the project actually builds. Later, replace fixed text with a variable to make the program respond to data.

Program.cs — two output lines

Console.WriteLine("Hello, Codectionary!");
Console.WriteLine("I wrote my first C# program.");

Why is there no Main method?

Modern console templates use top-level statements: you can write this short program directly in Program.cs, and the compiler generates the entry-point method. Older examples often show an explicit static Main method inside a class. Both styles are valid C# entry points. For this project, replace the whole file with the traditional version below if you want to try it; do not paste it underneath the existing top-level statements.

using System makes the Console type available explicitly. Program is a class; static Main is the method where execution starts in this version. The method body contains the same WriteLine call, so the output is unchanged. The SDK template commonly enables implicit usings, which is why the top-level version did not need an explicit using System line. Other C# application types and frameworks can use different startup patterns.

Alternative Program.cs — traditional entry point

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, Codectionary!");
    }
}

Recognise and fix common first-program errors

If dotnet new console says no SDK is installed, you may have installed only a runtime. Check dotnet --info and install the SDK. If dotnet run cannot find a project, cd into FirstCSharp or use dotnet run --project with a project path. If the old message still appears, save Program.cs and confirm that you opened the intended project folder.

A missing quote, closing parenthesis or semicolon produces a compiler error; read its file and line number, correct the source and run again. If Console is not recognised in a project with implicit usings disabled, add using System; at the top or write System.Console.WriteLine. A compiler error stops the build before your program runs; an exception reported after launch is a runtime problem. Work through the first error rather than changing unrelated lines.

Syntax

dotnet new console
dotnet run

Examples

A first C# greeting

A complete Program.cs for the .NET 10 console template using top-level statements.

Console.WriteLine("Hello, Codectionary!");
  1. Save this line in Program.cs inside a console project and run dotnet run from the project folder. The terminal prints Hello, Codectionary!.
  2. Console is the standard-library type; WriteLine is its method for writing a value followed by a newline. The string inside quotation marks is the value passed between the parentheses.
  3. The semicolon ends the statement. Try changing the string and rerun to see how source changes affect the output.

Best practices

  • Confirm that dotnet --info lists an SDK before starting.
  • Run dotnet run from the folder containing the .csproj file.
  • Save Program.cs before rerunning; use compiler file and line numbers to locate syntax errors.
  • Treat top-level statements and explicit Main as alternative entry-point styles.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading