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 PE32 or
PE32+
.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. The language calls one file a
compilation unit. 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");
}
}
The File structure page covers encoding, line endings, compilation units, using directives, top-level statements and partial files in full.
Language reference
The pages below describe the whole language. They follow the C# language specification as published on Microsoft Learn, and give its clause numbers. Features newer than the specification are described from their feature specifications. C# 14 is the latest released version. C# 15 ships with .NET 11, which was still a release candidate when these pages were written; its features are marked as C# 15.
- File structure – encoding, compilation units, using directives, global usings, file-scoped namespaces, top-level statements and partial files
- Lexical structure – comments and XML documentation, keywords, identifiers and every kind of literal, including verbatim, interpolated and raw strings
- Preprocessor – every directive, including
#if,#nullable,#regionand#pragma - Types – value and reference types, built-in types, nullability, boxing, conversions,
dynamic, tuples and unions - Variables – categories, definite assignment,
ref,inandout, and ref locals - Namespaces – declarations, the global namespace, extern aliases and name lookup
- Classes – members, constructors, properties, indexers, events, operators, and static and partial members
- Structs and records – structs, ref structs, inline arrays, record classes and record structs
- Interfaces and inheritance – virtual dispatch, abstract, sealed and closed classes, and default interface methods
- Enums and delegates – enum values and flags, delegates and anonymous methods
- Generics – type parameters, constraints and variance
- Expressions – the precedence table, null operators, ranges and indices,
with, lambdas and query syntax - Statements – every statement, including
using,lockandchecked - Patterns – every kind of pattern and switch expressions
- Exceptions –
throw,try, filters and the exception classes - Async and iterators –
asyncandawait, iterators and async streams - Attributes – using and declaring attributes, targets and the reserved attributes
- Unsafe code – pointers,
fixed,stackalloc, fixed-size buffers and function pointers - Standard library – the main .NET namespaces, briefly
- Versions – every C# version from 1.0 to C# 15, and what each added
Query words in the viewer
The viewer colours the words of a query expression (from, where,
select, group, by, orderby,
ascending, descending, join, on,
equals, let, into) only inside one. A query starts
at a from that is followed by a range variable (with or without a type) and
in, and ends at the semicolon at the same nesting level, or at the bracket
that closes the call, array or block it sits in. Outside a query these are ordinary
names, so var select = 1; and x.where show no keyword. A
where followed by a type parameter and a colon (where T : class)
is the generic constraint and is coloured as a keyword.
Not described
Known, not yet covered
- Reference: the standard library page is a map of the main namespaces and the types the language depends on. It does not describe the members of any library type (
Console,List<T>, the LINQ operators); the .NET API documentation does. - Reference: the pages describe the language, not the frameworks written in it: ASP.NET, Entity Framework, WPF and the like are not covered, and Roslyn analyser and source generator rules are described only as far as the attribute pages need.
- Viewer: only some contextual keywords are coloured.
get,set,init,required,partial,global,not,and,orandfieldare left plain, because they are ordinary names as often as not, so property accessors and patterns show only their other words.when,awaitandyieldare coloured wherever they appear, even as a name. A query expression nested in an interpolated string’s braces is part of the string’s colour. - Viewer: only a short list of library types is coloured as a type (
String,Object,Task,List,Dictionary,Exception, and the built-in types). Other types, user-declared types and attribute names such as[Obsolete]are plain, and the code inside the braces of an interpolated string is part of the string’s colour. - Viewer: the scanner does not compile: it reports no type, nullability, definite-assignment or syntax errors, does not evaluate
#if(code in a false branch is coloured as live) and does not followusingdirectives or project files. Only the first 1 MiB of a file is read and coloured.
Links
Software
- .NET SDK – Microsoft’s toolkit, with the C# compiler and the
dotnetcommand (Windows, macOS, Linux) - Visual Studio – Microsoft’s development environment for .NET (Windows)
- Visual Studio Code – free code editor with C# extensions (Windows, macOS, Linux)
- JetBrains Rider – JetBrains’ development environment for .NET (Windows, macOS, Linux)
Developers
- Roslyn – the C# compiler as a library, with APIs for parsing and analysing code (C#)
- tree-sitter-c-sharp – C# grammar for the Tree-sitter parser used by editors
Reference
- C# documentation – Microsoft Learn’s guide and language reference
- C# language specification – the specification these pages follow
- The history of C# – what each version of the language added
- dotnet/csharplang – feature specifications and the design of the language