Introduction
A .cs file holds source code in C#, Microsoft’s language for the
.NET platform. The C# compiler turns the
files of a project into a .NET assembly: a PE
.exe or .dll that holds bytecode for the .NET runtime. The
files that belong to a project, and the libraries it uses, are listed in a
.csproj project file.
C# is also the scripting language of the Unity game engine and of the Godot engine’s .NET edition.
Structure
A C# source file is plain text, usually UTF-8. It starts with using
directives, then a namespace and the types in it: classes, structs, interfaces, enums
and records. Unlike Java, a file may hold any number of public types with any names,
and a partial class can be split across several files. Since C# 9, one file
in a program may hold top-level statements instead of a Main method.
using System;
namespace Example;
public static class Program
{
public static void Main()
{
Console.WriteLine("Hello, world");
}
}
Links
Software
- .NET SDK, with the C# compiler and the
dotnetcommand (Windows, macOS, Linux) - Visual Studio (Windows)
- Visual Studio Code, with the C# extensions (Windows, macOS, Linux)
- JetBrains Rider (Windows, macOS, Linux)
Developers
- Roslyn, the C# compiler as a .NET library, with APIs for parsing and analysing code
- tree-sitter-c-sharp, C# grammar for the Tree-sitter parser used by editors
Reference
- C# documentation, Microsoft Learn