Creating and Running .cs Files
This page explains how to save C# code as a text file and run it. The file itself is simply a plain text file saved with a .cs extension.
How to write a .cs file
Write your C# code in a text editor and save the file with a .cs extension. Save the file using the UTF-8 character encoding. In C#, it is conventional to use PascalCase for file names (e.g. Program.cs, Calculator.cs).
Program.cs
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
In a C# program, the Main method is the entry point of execution. A project must have only one Main method. In modern C#, project management using the .NET CLI (dotnet command) is the standard approach.
How to write comments
You can write comments (notes) in a .cs file. Comments are ignored by the compiler, so they are useful for leaving descriptions or notes about the code.
| Syntax | Description |
|---|---|
| // comment | A single-line comment. Write it with one space after //. Everything from // to the end of the line is a comment. |
| /* comment */ | A multi-line comment. Everything from /* to */ is a comment. |
| /// comment | An XML documentation comment. When written immediately before a class or method, it is used by documentation generation tools. |
using System;
/// <summary>
/// Class containing the program's entry point.
/// </summary>
class Program
{
static void Main(string[] args)
{
/*
A multi-line comment.
Used to describe processing details.
*/
Console.WriteLine("Hello, World!");
}
}
/// is an XML documentation comment. When written immediately before a class or method, it appears as an IntelliSense description in IDEs such as Visual Studio. // comment is a regular single-line comment used to leave descriptions in code.
How to run
Running C# requires the .NET SDK. In modern development, using the .NET CLI (dotnet command) to manage projects is the standard approach.
Step 1: Create a console application project
The dotnet new console command creates a new console application project. Use the -n option to specify the project name. For details on this command, see Setup.
dotnet new console -n MyProject
The template "Console App" was created successfully.
A project directory is generated with Program.cs inside it.
cd MyProject
Step 2: Write code and run
Write your code in the auto-generated Program.cs and run it with dotnet run.
dotnet run
Hello, World!
Step 3: Working with multiple files
All .cs files in the project are automatically included in the build. You do not need to specify files as command arguments. For example, let's add a Hello.cs file.
Hello.cs
class Hello
{
public static void Greet()
{
string name = "C#";
int version = 12;
Console.WriteLine("Language: " + name);
Console.WriteLine("Version: " + version);
}
}
Call it from Program.cs. Update Program.cs as follows.
Program.cs
class Program
{
static void Main(string[] args)
{
Hello.Greet();
Console.WriteLine("Hello, World!");
}
}
The project file structure looks like this.
Run dotnet run.
dotnet run Language: C# Version: 12 Hello, World!
As shown, simply adding .cs files to the project directory automatically includes them in the build. However, the Main method (entry point) must exist only once in the project. Having multiple entry points will cause a compilation error.
Summary
A .cs file is simply a plain text file. You can create one by writing C# code in a text editor and saving it with a .cs extension.
Using the .NET CLI is the modern standard way to develop in C#. Create a project with dotnet new console and compile and run in one step with dotnet run. Because the project management file (.csproj) is generated automatically, dependency packages and settings can also be managed centrally with the dotnet command.
For recommended editors and environment setup, see Setup. That page also covers the differences between Debug and Release builds, as well as the directory structure.
If you find any errors or copyright issues, please contact us.